简体   繁体   English

为什么在使用 tensorflow 时出现警告/错误(使用功能 API 并且未实现错误)

[英]why am I getting warning/error when working with tensorflow (use functional API and not implemented error)

I am trying to follow this tutorial but with my data: https://www.tensorflow.org/tutorials/structured_data/feature_columns我正在尝试遵循本教程,但使用我的数据: https://www.tensorflow.org/tutorials/structured_data/feature_columns

All of my data is numerical values.我所有的数据都是数值。

when I ran this part of code:当我运行这部分代码时:

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])


history = model.fit(train_ds, validation_data=test_ds, epochs=100, use_multiprocessing=True)

I am getting this type of warning for all of the parameters:我收到所有参数的此类警告:

WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'dict'> input: {'age': <tf.Tensor 'ExpandDims_8:0' shape=(None, 1) dtype=int64>,

I am getting this warning twice for each variable!对于每个变量,我都会收到两次此警告!

and then I am getting this error:然后我收到此错误:

UnimplementedError:  Cast string to float is not supported
 [[node sequential_7/dense_features_7/calprotectin/Cast (defined at <ipython-input-103-5689ba5df442>:5) ]] [Op:__inference_train_function_4860]

What is the problem and how can I fix it?有什么问题,我该如何解决?

Edit1编辑1

I tried to mimic my code and error using sample data and I came up with this code.我试图使用示例数据来模仿我的代码和错误,然后我想出了这段代码。

The code doesn't generate an error but generates a warning.该代码不会产生错误,但会产生警告。 so the problem is with the data that I am reading.所以问题出在我正在阅读的数据上。 What can go wrong with the input data that generate such an error? go 输入数据有什么错误会产生这样的错误?

( it is a jupyter code, how can I post it here?): (这是一个jupyter代码,我怎么能把它贴在这里?):

%reset
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow import feature_column
from sklearn.model_selection import train_test_split

RANDOM_SEED = 42

data=pd.DataFrame()
data['sex']=[1,2,2,1,2,2,1,1,2,1]
data['age']=[10,11,13,45,67,34,23,62,82,78]
data['bmi']=[22.5,28.8,19,23.3,26,18.4,27.5,29,30.3,25.9]
data['smoker']=[1,2,2,3,3,2,2,1,1,1]
data['lab1']=[144,124,126,146,130,124,171,147,131,138]
data['lab2']=[71,82,75,65,56,89,55,74,78,69]
data['result']=[1,2,2,4,3,2,1,3,2,4]

feature_columns = []
for header in ['sex','age', 'bmi','smoker', 'lab1', 'lab2']:
  feature_columns.append(tf.feature_column.numeric_column(header))

def create_dataset(dataframe, batch_size=32):
    dataframe = dataframe.copy()
    labels = dataframe.pop('result')
    return tf.data.Dataset.from_tensor_slices((dict(dataframe), labels)) \
      .shuffle(buffer_size=len(dataframe)) \
      .batch(batch_size)

train, test = train_test_split(data, test_size=0.2, random_state=RANDOM_SEED)
train_ds = create_dataset(train)
test_ds = create_dataset(test)

model = tf.keras.models.Sequential([
  tf.keras.layers.DenseFeatures(feature_columns=feature_columns),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(.1),
  tf.keras.layers.Dense(1)
])

model.compile(optimizer='adam',
          loss='binary_crossentropy',
          metrics=['accuracy'])

history = model.fit(train_ds, validation_data=test_ds, epochs=100, use_multiprocessing=True)

when I run the above code, I am getting this warning:当我运行上面的代码时,我收到了这个警告:

Epoch 1/100
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'dict'> input: {'sex': <tf.Tensor 'ExpandDims_4:0' shape=(None, 1) dtype=int64>, 'age': <tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=int64>, 'bmi': <tf.Tensor 'ExpandDims_1:0' shape=(None, 1) dtype=float64>, 'smoker': <tf.Tensor 'ExpandDims_5:0' shape=(None, 1) dtype=int64>, 'lab1': <tf.Tensor 'ExpandDims_2:0' shape=(None, 1) dtype=int64>, 'lab2': <tf.Tensor 'ExpandDims_3:0' shape=(None, 1) dtype=int64>}
Consider rewriting this model with the Functional API.
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'dict'> input: {'sex': <tf.Tensor 'ExpandDims_4:0' shape=(None, 1) dtype=int64>, 'age': <tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=int64>, 'bmi': <tf.Tensor 'ExpandDims_1:0' shape=(None, 1) dtype=float64>, 'smoker': <tf.Tensor 'ExpandDims_5:0' shape=(None, 1) dtype=int64>, 'lab1': <tf.Tensor 'ExpandDims_2:0' shape=(None, 1) dtype=int64>, 'lab2': <tf.Tensor 'ExpandDims_3:0' shape=(None, 1) dtype=int64>}
Consider rewriting this model with the Functional API.
1/1 [==============================] - ETA: 0s - loss: -22.8739 - accuracy: 0.2500WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor, but we receive a <class 'dict'> input: {'sex': <tf.Tensor 'ExpandDims_4:0' shape=(None, 1) dtype=int64>, 'age': <tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=int64>, 'bmi': <tf.Tensor 'ExpandDims_1:0' shape=(None, 1) dtype=float64>, 'smoker': <tf.Tensor 'ExpandDims_5:0' shape=(None, 1) dtype=int64>, 'lab1': <tf.Tensor 'ExpandDims_2:0' shape=(None, 1) dtype=int64>, 'lab2': <tf.Tensor 'ExpandDims_3:0' shape=(None, 1) dtype=int64>}
Consider rewriting this model with the Functional API.

When model fit finished, the accuracy is zero.当 model 拟合完成时,精度为零。 I know that the data is not valid, bit having an accuracy of zero is also not expected.我知道数据无效,也不期望精度为零的位。

The reason you have no improvement when training the model because you using BinaryCrossentropy loss for multi-labels, deal this error wrt following two case训练 model 时没有改进的原因是因为您对多标签使用BinaryCrossentropy损失,请在以下两种情况下处理此错误

  1. For binary classification:对于二进制分类:

    Let, eg data['result']=[1,0,0,1,0,0,1,0,0,1] and using loss=tf.keras.losses.BinaryCrossentropy(from_logits=True)让,例如data['result']=[1,0,0,1,0,0,1,0,0,1]并使用loss=tf.keras.losses.BinaryCrossentropy(from_logits=True)

  2. For multiclass classification:对于多类分类:

    Using loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) , and modify the output layer of model so it outputs shape match the number of labels, eg tf.keras.layers.Dense(5) when you have 5 classes Using loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) , and modify the output layer of model so it outputs shape match the number of labels, eg tf.keras.layers.Dense(5) when you have 5 classes

That WARNING from tf.keras.models.Sequential() is simply telling you what it expect from layer within it in order for it to work properly, if you don't using tf.keras.models.Sequential() then WARNING will disappear, eg define model using:来自tf.keras.models.Sequential()WARNING只是告诉您它对其中的层的期望以使其正常工作,如果您不使用tf.keras.models.Sequential()那么WARNING将消失,例如定义 model 使用:

inputs = {}
for header in ['sex','age', 'bmi','smoker', 'lab1', 'lab2']:
    inputs[header] = tf.keras.Input(shape=(1,), name=header) 
x = tf.keras.layers.DenseFeatures(feature_columns=feature_columns)(inputs)
x = tf.keras.layers.Dense(128, activation='relu')(x)
x = tf.keras.layers.Dense(128, activation='relu')(x)
x = tf.keras.layers.Dropout(.1)(x)
x = tf.keras.layers.Dense(1)(x)
model = tf.keras.models.Model(inputs=inputs, outputs=x)

The reason you get that error of Cast string to float probably due to you try to convert all the columns to numeric column like you did in that example code you post (ie maybe better to convert sex columns to categorical columns )您收到Cast string to float错误的原因可能是因为您尝试将所有列转换为numeric column ,就像您在发布的示例代码中所做的那样(即,将sex列转换为categorical columns可能更好)

To fix this warning, you have 2 ways:要修复此警告,您有两种方法:

1] Apply features layer in the input when you call for fit method. 1] 当您调用 fit 方法时,在输入中应用特征层。 ie instead of:即代替:

model3.fit(x=train_dict, y=train_labels, validation_data=(valid_dict,valid_labels), epochs=epochs_, verbose=verbose_)

use利用

model3.fit(x=feature_layer_3(train_dict), y=train_labels, validation_data=(feature_layer_3(valid_dict),valid_labels), epochs=epochs_, verbose=verbose_)

you could check this detailed example (3rd Model) https://www.kaggle.com/abidou/features-bucketing您可以查看此详细示例(第 3 款) https://www.kaggle.com/abidou/features-bucketing

2] Use Functional API as in 6th model in previous link 2] 使用功能 API,如上一个链接中的第 6 个 model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在运行此脚本时收到 tensorflow 警告? - why am i getting a tensorflow warning when running this script? 为什么在 python 实现的算法中出现逻辑错误 - Why am I getting a logical error in a python implemented algorithm 为什么在使用 Twitter Rest API 时会出现此错误 - Why am I getting this error when using Twitter Rest API 为什么从.csv打印(行)时,为什么在python中出现W292错误(警告)? - Why I am getting W292 error(warning) in python when print(rows) from a .csv? 为什么我得到Tensorflow服务模块导入错误? - Why am I getting Tensorflow Serving module import error? 我在pycharm上得到Tensorflow安装错误 - I am getting tensorflow installation error on pycharm tensorflow keras:我收到此错误“模块“tensorflow._api.v1.keras.layers”没有属性“flatten” - tensorflow keras: I am getting this error 'module "tensorflow._api.v1.keras.layers' has no attribute 'flatten'" 为什么从 API 查询时会收到 400 错误? - Why am I getting 400 error when I query from an API? 为什么我会收到此 TensorFlow 错误? - Why am I receiving this TensorFlow Error? 使用2维卷积输入层时,为什么会出现尺寸错误? - Why am I getting a dimensional error when I use a 2-dim convolutional input layer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM