来自 TF 的 Keras:损失是 NaN 并且无法找到可以处理输入的数据适配器:<class 'pandas.core.frame.DataFrame'>、<class 'NoneType'>
Keras from TF : loss is NaN and Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>
我试图找到一些应该可以解决我的问题的解决方案,但目前 none 正在工作。 (喜欢)
我正在通过 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'>
但通常我的输入和输出都是<class 'pandas.core.frame.DataFrame'>
而我的损失是loss: nan
。这有点令人沮丧,因为我不知道我的错误从何而来。
如果您知道出了什么问题?如果它太混乱,我可以提供我的代码。
提前致谢!
编辑:根据要求,这是我的代码:
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'>
我试图找到一些应该可以解决我的问题的解决方案,但目前 none 正在工作。 (喜欢
我正在通过 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'>
但通常我的输入和输出都是<class 'pandas.core.frame.DataFrame'>
而我的损失是loss: nan
。这有点令人沮丧,因为我不知道我的错误从何而来。
如果您知道出了什么问题?如果它太混乱,我可以提供我的代码。
提前致谢!
编辑:根据要求,这是我的代码:
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'>