TypeError:输入一层应进行张量
我是深度学习的新手,目前正在尝试学习神经网络。但是,我在训练神经网络时遇到了这个问题。
这是输入。我通过使用张量数据集思考,我准备将值传递到我构建的模型中。
train_dataset = tf.data.Dataset.from_tensor_slices((train.values, trainLabel.values))
test_dataset = tf.data.Dataset.from_tensor_slices((test.values, testLabel.values))
cv_dataset = tf.data.Dataset.from_tensor_slices((val.values, valLabel.values))
for features, targets in train_dataset.take(5):
print ('Features: {}, Target: {}'.format(features, targets))
这是上面的打印方法显示的输出:
Features: [ 0 40 0 0 0 1 31 33 17], Target: 29
Features: [ 0 32 0 1 0 1 50 55 44], Target: 7
Features: [ 0 32 1 0 1 1 12 43 31], Target: 34
Features: [ 0 29 1 1 1 0 56 52 37], Target: 14
Features: [ 0 25 0 0 1 1 29 30 15], Target: 17
这是我使用Keras API的模型:
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(9,)), # input shape required
tf.keras.layers.Dense(10, activation=tf.nn.relu),
tf.keras.layers.Dense(3)
])
我正在尝试在训练神经网络之前预览输出。
predictions = model(train_dataset)
predictions[:5]
但是,我有一个错误:
TypeError: Inputs to a layer should be tensors. Got: <BatchDataset element_spec=(TensorSpec(shape=(None, 9), dtype=tf.int64, name=None), TensorSpec(shape=(None,), dtype=tf.int64, name=None))>
我谷歌搜索以搜索错误并找到了这条代码行,但至少对我来说仍然不起作用
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(32)
I am new to deep learning currently trying to learn neural network.However,I encountered this problem while training the neural network.
This is the input .I thought by using the tensor Dataset I am ready to pass the values into the model I build.
train_dataset = tf.data.Dataset.from_tensor_slices((train.values, trainLabel.values))
test_dataset = tf.data.Dataset.from_tensor_slices((test.values, testLabel.values))
cv_dataset = tf.data.Dataset.from_tensor_slices((val.values, valLabel.values))
for features, targets in train_dataset.take(5):
print ('Features: {}, Target: {}'.format(features, targets))
This is the output shown from the print method above:
Features: [ 0 40 0 0 0 1 31 33 17], Target: 29
Features: [ 0 32 0 1 0 1 50 55 44], Target: 7
Features: [ 0 32 1 0 1 1 12 43 31], Target: 34
Features: [ 0 29 1 1 1 0 56 52 37], Target: 14
Features: [ 0 25 0 0 1 1 29 30 15], Target: 17
This is my model using Keras API:
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(9,)), # input shape required
tf.keras.layers.Dense(10, activation=tf.nn.relu),
tf.keras.layers.Dense(3)
])
I am trying to preview the output before training the neural network.
predictions = model(train_dataset)
predictions[:5]
However, I got this error :
TypeError: Inputs to a layer should be tensors. Got: <BatchDataset element_spec=(TensorSpec(shape=(None, 9), dtype=tf.int64, name=None), TensorSpec(shape=(None,), dtype=tf.int64, name=None))>
I googled myself to search for the error and found this line of code but still not working, at least for me
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(32)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此错误的原因是,您向模型提供
batchdataset
。相反,您必须将输入数据更改为数组,然后将其馈送到模型中并进行模型定义并编译模型。以下代码只会为您提供已经存在的数据集的预览,而没有任何更改:
因此,您需要使用输入数据训练模型,然后检查预测:
输出:
注:我已经使用了鲍鱼数据集复制此代码。
The reason for this error, you're providing
BatchDataset
to the model. Instead, you must change the input data into an array before feeding that into the model and do the model definition and compile the model.The below code will give you only a preview of an already existing dataset without any changes:
Hence, you need to train the model with input data and then check for predictions:
Output:
Note: I have used the abalone dataset to replicate this code.