来自 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'>

查看:103
本文介绍了来自 TF 的 Keras:损失是 NaN 并且无法找到可以处理输入的数据适配器:<class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一些应该可以解决我的问题的解决方案,但目前它们都不起作用.(例如 Tensorflow ValueError: Failed to find data adapter可以处理输入)

I tried to find some solutions that should solves my problem but for the moment none of them are working. (like Tensorflow ValueError: Failed to find data adapter that can handle input)

我正在使用具有输入形状的自定义数据集通过 Keras(来自 TF)进行神经网络: (5000, 1) 和输出形状 (5000, 16).输入是时间和周期数,输出是 16 个灯中每一个的状态(0 表示关闭或 1 表示打开).我使用 Adam 作为优化器,我的损失是categorical_crossentropy"(也许我在使用这个时犯了一个错误......我不确定).

I'm doing a neural network through Keras (from TF) using a custom dataset with an input shape : (5000, 1) and output shape of (5000, 16). The inputs are the time and the cycle number and the outputs are the state of each of the 16 lamps (either 0 for off or 1 for on). I use Adam as the optimizer and my loss is 'categorical_crossentropy' (maybe I did an error using this one ... I not sure of it).

所以问题是当我尝试训练我的网络时,我收到此错误消息:

So the problem is when I try to train my network I have this error message :

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'>

但通常我的输入和输出都是

But normally both of my input and output are <class 'pandas.core.frame.DataFrame'>

而我的损失是loss: nan.这有点令人沮丧,因为我不知道我的错误来自哪里.

And my loss is loss: nan. This is a bit frustrating since I don't know from where my error can come.

如果您知道出了什么问题?如果太混乱,我可以提供我的代码.

If you have any idea of what's wrong ? I can provide my code if it's too confusing.

先谢谢你!

如所问,这是我的代码:

EDIT : As asked, here's my code :

    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,而是为每行做 1 nn.

First do the feature scaling by using StandardScaler from sklearn.preprocessing (for inputs and outputs). Then instead of doing 1 nn for all the lines, I do one nn for each.

我仍然不知道为什么我收到错误消息Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>

Still I don't know why I had the error message "Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>

这篇关于来自 TF 的 Keras:损失是 NaN 并且无法找到可以处理输入的数据适配器:&lt;class 'pandas.core.frame.DataFrame'&gt;, &lt;class 'NoneType'&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆