簡體   English   中英

張量輸入形狀與模型擬合的問題

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

我正在運行一個簡單的神經網絡接口。

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

輸入形狀為 [10,1] 的張量,這意味着值和標簽。 當我擬合模型時,出現錯誤:

output_shape = [product, shape[-1]] IndexError:列表索引超出范圍

我正在使用 tensorflow 2.0

這是輸出錯誤:

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.

你能提供model.fit調用嗎? 這是一組工作代碼。 工作,這意味着它不會使用 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)

您得到的錯誤可能是因為您沒有向model.fit提供y

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM