简体   繁体   English

TensorFlow - ValueError:形状 (3, 1) 和 (4, 3) 不兼容

[英]TensorFlow - ValueError: Shapes (3, 1) and (4, 3) are incompatible

I'm completely new to DL and I'm stuck with this error when I fit my model我是 DL 的新手,当我安装 model 时,我遇到了这个错误

ValueError: Shapes (3, 1) and (4, 3) are incompatible

Dataset:数据集:

Features: [0.22222222 0.625      0.06779661 0.04166667], Target: [1 0 0]
Features: [0.16666667 0.41666667 0.06779661 0.04166667], Target: [1 0 0]
Features: [0.11111111 0.5        0.05084746 0.04166667], Target: [1 0 0]
Features: [0.08333333 0.45833333 0.08474576 0.04166667], Target: [1 0 0]
Features: [0.19444444 0.66666667 0.06779661 0.04166667], Target: [1 0 0]

Model: Model:

def build_fc_model():
  fc_model = tf.keras.Sequential([
      tf.keras.layers.Dense(4, activation=tf.nn.softmax),
      tf.keras.layers.Dense(4, activation=tf.nn.softmax),
      tf.keras.layers.Dense(3, activation=tf.nn.softmax),
  ])
  return fc_model```

Error at model.fit错误在 model.fit

model = build_fc_model()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-1), loss='categorical_crossentropy', metrics=['accuracy'])

BATCH_SIZE = 10
EPOCHS = 5

model.fit(dataset, batch_size=BATCH_SIZE, epochs=EPOCHS)

Thanks for any help谢谢你的帮助

In your code InputLayer is missing in build_fc_model so check this out:在您的代码中, build_fc_model中缺少InputLayer ,因此请检查一下:

import tensorflow as tf
import numpy as np

def build_fc_model():
  fc_model = tf.keras.Sequential([
      tf.keras.layers.InputLayer((4,)),
      tf.keras.layers.Dense(4, activation=tf.nn.softmax),
      tf.keras.layers.Dense(4, activation=tf.nn.softmax),
      tf.keras.layers.Dense(3, activation=tf.nn.softmax),
  ])
  return fc_model


data = np.array([[0.22222222, 0.625,      0.06779661, 0.04166667], 
                 [0.16666667, 0.41666667, 0.06779661, 0.04166667],
                 [0.11111111, 0.5 ,       0.05084746, 0.04166667], 
                 [0.08333333, 0.45833333, 0.08474576, 0.04166667], 
                 [0.19444444, 0.66666667, 0.06779661, 0.04166667]])

target = np.array([[1, 0 ,0],
                   [1, 0 ,0],
                   [1, 0 ,0],
                   [1, 0 ,0],
                   [1, 0 ,0]])

model = build_fc_model()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-1), loss='categorical_crossentropy', metrics=['accuracy'])

BATCH_SIZE = 1
EPOCHS = 5

model.fit(data, target, batch_size=BATCH_SIZE, epochs=EPOCHS)

Output: Output:

Epoch 1/5
5/5 [==============================] - 0s 991us/step - loss: 0.8198 - accuracy: 0.6000
Epoch 2/5
5/5 [==============================] - 0s 603us/step - loss: 0.1590 - accuracy: 1.0000
Epoch 3/5
5/5 [==============================] - 0s 593us/step - loss: 0.0372 - accuracy: 1.0000
Epoch 4/5
5/5 [==============================] - 0s 597us/step - loss: 0.0131 - accuracy: 1.0000
Epoch 5/5
5/5 [==============================] - 0s 680us/step - loss: 0.0064 - accuracy: 1.0000

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

相关问题 TensorFlow:ValueError:形状不兼容 - TensorFlow: ValueError: Shapes are incompatible Tensorflow ValueError:形状(?,1)和(?,)不兼容 - Tensorflow ValueError: Shapes (?, 1) and (?,) are incompatible Tensorflow ValueError 形状不兼容 - Tensorflow ValueError Shapes are incompatible tensorflow 值错误:形状不兼容 - tensorflow ValueError: Shapes are incompatible Tensorflow:ValueError:形状(None,1)和(None,2)不兼容 - Tensorflow: ValueError: Shapes (None, 1) and (None, 2) are incompatible TensorFlow - ValueError:形状(无,1)和(无,10)不兼容 - TensorFlow - ValueError: Shapes (None, 1) and (None, 10) are incompatible ValueError:使用 RandomizedSearchCV 的 Tensorflow LSTM 中的形状不兼容 - ValueError: Shapes are incompatible in Tensorflow LSTM using RandomizedSearchCV 在 tensorflow 中出现 ValueError 说我的形状不兼容 - Getting a ValueError in tensorflow saying that my shapes are incompatible Tensorflow 尺寸问题:ValueError:形状 (3, 1) 和 (None, 3) 不兼容 - Tensorflow dimension issue: ValueError: Shapes (3, 1) and (None, 3) are incompatible Tensorflow - 我没有得到正确的形状 - `ValueError:形状 (9, 1) 和 (8, 9) 不兼容` - Tensorflow - I don't get the right shapes - `ValueError: Shapes (9, 1) and (8, 9) are incompatible`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM