简体   繁体   English

使用子类化 API 时 Tensorflow 中的 ValueError

[英]ValueError in Tensorflow while using the subclassing API

I'm new into tensorflow and I am trying to learn how to use the subclassing API on a batch of data coming from fashion MNIST.我是 tensorflow 的新手,我正在尝试学习如何对来自时尚 MNIST 的一批数据使用子类化 API。 But I keep getting the same shape error over and over again and I can't find where it's coming from.但是我一遍又一遍地收到相同的形状错误,我找不到它的来源。 Here is my code :这是我的代码:


import tensorflow as tf
from tensorflow import keras

import os
import sys
assert sys.version_info >= (3, 5)
import sklearn
assert sklearn.__version__ >= "0.20"

import numpy as np
import matplotlib

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test = X_test / 255.
#Note that I am not using this part of the code for now because I
#need to debug it first:

#class Block(keras.layers.Layer):
#    def __init__(self, n_layers, n_neurons, **kwargs):
#        super().__init__(**kwargs)
#        self.hidden = [keras.layers.Dense(n_neurons, #activation="relu")
#                       for _ in range(n_layers)]

#    def call(self, inputs):
#        Z = inputs
#        for layer in self.hidden:
#            Z = layer(Z)
#        return inputs + Z
class Modeling(keras.Model):
  def __init__(self, **kwargs):
      super().__init__(**kwargs)
      self.Flatten = keras.layers.Flatten(input_shape=[28,28])
      self.dense1 = keras.layers.Dense(30, activation="relu")
      self.out = keras.layers.Dense(10, activation="softmax")

  def call(self, inputs):
    Z = self.dense1(self.Flatten(inputs))
#    for _ in range (1 + 3):
#      Z = self.block1(Z)
#    Z = self.block2(Z)
    return self.out(Z)
#This one shows that there is multiple output_shapes : 
model = Modeling()
model.build([28,28])
model.summary()
model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid))

The message I keep getting is, in short :简而言之,我不断收到的信息是:

ValueError值错误
Traceback 1 model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy']) 2 ----> 3 model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid)) Traceback 1 model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy']) 2 ----> 3 model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid))

    ValueError: Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 28, but received input with shape (None, 784)


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 28, 28), dtype=float32)

Any help and/or explanations on these input shapes would be greatly appreciated.对于这些输入形状的任何帮助和/或解释将不胜感激。 Thank you all!谢谢你们!

The error message itself tells you that your input is not in the shape as your defined model expects it.错误消息本身会告诉您您的输入不是您定义的模型所期望的形状。 So the dense layer is expecting input data with a certain number of features but got data that differs.因此,密集层期望输入数据具有一定数量的特征,但得到的数据不同。

I am not sure if it is important for you to use the structure you used here:我不确定使用此处使用的结构对您是否重要:

class Modeling(keras.Model):
  def __init__(self, **kwargs):
      super().__init__(**kwargs)
      self.Flatten = keras.layers.Flatten(input_shape=[28,28])
      self.dense1 = keras.layers.Dense(30, activation="relu")
      self.out = keras.layers.Dense(10, activation="softmax")

  def call(self, inputs):
    Z = self.dense1(self.Flatten(inputs))
#    for _ in range (1 + 3):
#      Z = self.block1(Z)
#    Z = self.block2(Z)
    return self.out(Z)

When I replace this with the following:当我用以下内容替换它时:

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(30, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

It works.有用。

Full working code:完整的工作代码:

import tensorflow as tf
from tensorflow import keras

import os
import sys


import numpy as np
import matplotlib

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()

X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test = X_test / 255.

model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),
    keras.layers.Dense(30, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

model.compile(optimizer="sgd", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model.fit(X_train, y_train, epochs=1, validation_data=(X_valid, y_valid))

Gives me loss: 0.8309 - acc: 0.7282 - val_loss: 0.5908 - val_acc: 0.7994给我loss: 0.8309 - acc: 0.7282 - val_loss: 0.5908 - val_acc: 0.7994

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM