简体   繁体   English

张量输入形状与模型拟合的问题

[英]Problem with tensor input shape wile fitting a model

Im running a simple neural network interface.我正在运行一个简单的神经网络接口。

model = tf.keras.models.Sequential([
  #tf.keras.layers.Flatten(input_shape=(10, 1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(2, activation='softmax')
])

With a given loss function:给定损失函数:

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

Feeding in a tensor of shape [10,1], that means values and labels.输入形状为 [10,1] 的张量,这意味着值和标签。 When I fit the model i get an error:当我拟合模型时,出现错误:

output_shape = [product, shape[-1]] IndexError: list index out of range output_shape = [product, shape[-1]] IndexError:列表索引超出范围

I`m using tensorflow 2.0我正在使用 tensorflow 2.0

Here is the output error:这是输出错误:

If using Keras pass *_constraint arguments to layers.
Train on 200000 steps
Epoch 1/5
2020-02-02 12:48:13.088278: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
Traceback (most recent call last):
  File "MLP.py", line 121, in <module>
    model.fit(train, epochs=5)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 727, in fit
    use_multiprocessing=use_multiprocessing)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 675, in fit
    steps_name='steps_per_epoch')
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 300, in model_iteration
    batch_outs = f(actual_inputs)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py", line 3476, in __call__
    run_metadata=self.run_metadata)
  File "/home/jacek/anaconda3/envs/alice_tf/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1472, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: Expected dimension in the range [0, 0), but got -1
     [[{{node metrics/acc/ArgMax}}]]
     [[loss/mul/_59]]
  (1) Invalid argument: Expected dimension in the range [0, 0), but got -1
     [[{{node metrics/acc/ArgMax}}]]
0 successful operations.
0 derived errors ignored.

Can you provide the model.fit call?你能提供model.fit调用吗? Here is a working set of code.这是一组工作代码。 Working, meaning it does not raise errors using tensorflow 2.0 or 2.1.工作,这意味着它不会使用 tensorflow 2.0 或 2.1 引发错误。

import numpy as np
import tensorflow as tf

model = tf.keras.models.Sequential([
  #tf.keras.layers.Flatten(input_shape=(10, 1)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(2, activation='softmax')
])

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

# Generate fake data.
x = np.ones([10, 1], dtype=np.float32)
y = np.ones([10, 2], dtype=np.float32)

# Train the model, providing features and labels.
model.fit(x, y)

The error you are getting could be because you are not providing y to model.fit .您得到的错误可能是因为您没有向model.fit提供y

暂无
暂无

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

相关问题 拟合model时ImageDataGenerator的形状问题 - Shape problem of ImageDataGenerator when fitting the model Keras LSTM 模型中的输入形状和拟合 - The input shape and fitting in Keras LSTM model 如何获取未知 PyTorch model 的输入张量形状 - How to get input tensor shape of an unknown PyTorch model Tensorflow 模型是用输入 Tensor 的形状构建的,但它在形状不兼容的输入(神经网络)上被调用 - Tensorflow model was constructed with shape for input Tensor but it was called on an input with incompatible shape (Neural Network) 警告:tensorflow:Model 是用输入 Tensor() 的形状构建的。 但它是在形状不兼容的输入上调用的 - WARNING:tensorflow:Model was constructed with shape for input Tensor(). but it was called on an input with incompatible shape Tensor模型输入-nvalidArgumentError(请参阅上面的回溯):shape_and_slice规范中的形状 - Tensor model input - nvalidArgumentError (see above for traceback): Shape in shape_and_slice spec 警告:tensorflow:模型是用形状 (20, 37, 42) 构建的,用于输入 Tensor(“input_5:0”, shape=(20, 37, 42), dtype=float32),但是 - WARNING:tensorflow:Model was constructed with shape (20, 37, 42) for input Tensor(“input_5:0”, shape=(20, 37, 42), dtype=float32), but Tensorflow/Keras - 重塑问题(重塑的输入是一个有 10 个值的张量,但请求的形状有 1 个) - Tensorflow/Keras - Reshaping problem (Input to reshape is a tensor with 10 values, but the requested shape has 1) 拟合我的模型时出现 ValueError。 (ValueError:无法将输入数组从形状(224,224,3)广播到形状(224,224,3,3)) - ValueError when fitting my model. (ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224,3,3)) Bert DL 模型错误:重塑的输入是具有 3200 个值的张量,但请求的形状具有 3328 - Bert DL model Error: Input to reshape is a tensor with 3200 values, but the requested shape has 3328
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM