TypeError:输入一层应进行张量

发布于 2025-02-06 07:55:19 字数 1566 浏览 3 评论 0原文

我是深度学习的新手,目前正在尝试学习神经网络。但是,我在训练神经网络时遇到了这个问题。

这是输入。我通过使用张量数据集思考,我准备将值传递到我构建的模型中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2025-02-13 07:55:19

此错误的原因是,您向模型提供batchdataset。相反,您必须将输入数据更改为数组,然后将其馈送到模型中并进行模型定义并编译模型。

train = np.array(train)
train

train_dataset = tf.data.Dataset.from_tensor_slices((train, trainLabel)

for features, targets in train_dataset.take(5):
  print ('Features: {}, Target: {}'.format(features, targets))

train_dataset = train_dataset.shuffle(buffer_size=1024).batch(32) 
#type(train_dataset) --->tensorflow.python.data.ops.dataset_ops.BatchDataset

以下代码只会为您提供已经存在的数据集的预览,而没有任何更改:

predictions = model(train)
predictions[:5]

因此,您需要使用输入数据训练模型,然后检查预测:

model.fit(train_dataset, epochs=10)

predictions = model(train)
predictions[:5]

输出:

<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[ 7.51 ],
       [10.033],
       [11.349],
       [ 9.586],
       [ 9.86 ]], dtype=float32)>

注:我已经使用了鲍鱼数据集复制此代码。

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.

train = np.array(train)
train

train_dataset = tf.data.Dataset.from_tensor_slices((train, trainLabel)

for features, targets in train_dataset.take(5):
  print ('Features: {}, Target: {}'.format(features, targets))

train_dataset = train_dataset.shuffle(buffer_size=1024).batch(32) 
#type(train_dataset) --->tensorflow.python.data.ops.dataset_ops.BatchDataset

The below code will give you only a preview of an already existing dataset without any changes:

predictions = model(train)
predictions[:5]

Hence, you need to train the model with input data and then check for predictions:

model.fit(train_dataset, epochs=10)

predictions = model(train)
predictions[:5]

Output:

<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[ 7.51 ],
       [10.033],
       [11.349],
       [ 9.586],
       [ 9.86 ]], dtype=float32)>

Note: I have used the abalone dataset to replicate this code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文