来自TF的Keras:损失是NaN并且无法找到可以处理输入的数据适配器:<class \\’pandas.core.frame.DataFrame\\’>,<class \\’NoneType\\’>
•浏览 1
Keras from TF : loss is NaN and Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>
我试图找到一些可以解决我的问题的解决方案,但目前它们都不起作用。 (如 Tensorflow ValueError: Failed to find data adapter that can handle input)
我正在通过 Keras(来自 TF)使用具有输入形状: (5000, 1) 和输出形状为 (5000, 16) 的自定义数据集进行神经网络。
输入是时间和周期数,输出是 16 个灯中每个灯的状态(0 表示关闭或 1 表示打开)。我使用 Adam 作为优化器,我的损失是"categorical_crossentropy"(也许我在使用这个时出错了……我不确定)。
所以问题是当我尝试训练我的网络时,我收到了以下错误消息:
WARNING:tensorflow:Falling back from v2 loop because of error: Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
# Read csv file into a pandas dataframe
data= pd.read_excel(r'/datasetV07clear16lamps.xlsx')
#Index by time
data.sort_values("Time")
print(data.isnull().any() )
#split the dataset
train=data[0:5000]
test=data[5000:]
print(train.shape)
print(test.shape)
## split the dataset into train and test dataset
# create train dataset
X1_train=train[['Time']]
X2_train=train[['cycle']]
y_train=train[['L1green','L1orange','L1red','L1blink','L2green','L2orange','L2red','L2blink','L3green','L3orange','L3red','L3blink','L4green','L4orange','L4red','L4blink']]
#create test dataset
X1_test=test[['Time']]
X2_test=test[['cycle']]
y_test=test[['L1green','L1orange','L1red','L1blink','L2green','L2orange','L2red','L2blink','L3green','L3orange','L3red','L3blink','L4green','L4orange','L4red','L4blink']]
# Define the input
input_tensor = Input(shape=(2,))
# Define the output
output_tensor = Dense(16)(input_tensor)
# Create a model
model = Model(input_tensor, output_tensor)
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy')
# Fit the model
model.fit(train[['Time','cycle']], train[['L1green','L1orange','L1red','L1blink','L2green','L2orange','L2red','L2blink','L3green','L3orange','L3red','L3blink','L4green','L4orange','L4red','L4blink']], verbose=True, batch_size=16384, epochs=100)
但通常我的输入和输出都是 <class 'pandas.core.frame.DataFrame'>
而我的损失是loss: nan。这有点令人沮丧,因为我不知道我的错误来自哪里。
如果你知道什么是错的?如果太混乱,我可以提供我的代码。
提前谢谢你!
编辑:按照要求,这是我的代码:
WARNING:tensorflow:Falling back from v2 loop because of error: Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
# Read csv file into a pandas dataframe
data= pd.read_excel(r'/datasetV07clear16lamps.xlsx')
#Index by time
data.sort_values("Time")
print(data.isnull().any() )
#split the dataset
train=data[0:5000]
test=data[5000:]
print(train.shape)
print(test.shape)
## split the dataset into train and test dataset
# create train dataset
X1_train=train[['Time']]
X2_train=train[['cycle']]
y_train=train[['L1green','L1orange','L1red','L1blink','L2green','L2orange','L2red','L2blink','L3green','L3orange','L3red','L3blink','L4green','L4orange','L4red','L4blink']]
#create test dataset
X1_test=test[['Time']]
X2_test=test[['cycle']]
y_test=test[['L1green','L1orange','L1red','L1blink','L2green','L2orange','L2red','L2blink','L3green','L3orange','L3red','L3blink','L4green','L4orange','L4red','L4blink']]
# Define the input
input_tensor = Input(shape=(2,))
# Define the output
output_tensor = Dense(16)(input_tensor)
# Create a model
model = Model(input_tensor, output_tensor)
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy')
# Fit the model
model.fit(train[['Time','cycle']], train[['L1green','L1orange','L1red','L1blink','L2green','L2orange','L2red','L2blink','L3green','L3orange','L3red','L3blink','L4green','L4orange','L4red','L4blink']], verbose=True, batch_size=16384, epochs=100)
我找到了解决方案:
首先使用 sklearn.preprocessing 中的 StandardScaler 进行特征缩放(用于输入和输出)。然后我不是为所有行做 1 nn,而是为每个行做一个 nn。
我仍然不知道为什么我收到错误消息 "Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>