【问题标题】:Problem with training cifar10 data in Tensorflow-2在 Tensorflow-2 中训练 cifar10 数据的问题
【发布时间】:2019-11-27 19:15:12
【问题描述】:

在 tensorflow-2 中训练 cifar10 数据时出现以下错误。我用了这个tutorial

TypeError:预期的 float32 传递给 op 'Equal' 的参数 'y', 取而代之的是“str”类型的“集合”。错误:预期的 float32,得到 'str' 类型的 'collections' 代替。

我的代码如下:

    class Mymodel(tf.keras.Model):

        def __init__(self, class_size):
            """Initialize parameters and build model.
            """
            super(Mymodel, self).__init__()

            self.class_size =class_size
            self.conv1 = tf.keras.layers.Conv2D(32, kernel_size =3, strides =2, activation='relu')
            self.conv2 = tf.keras.layers.Conv2D(64, kernel_size =2, strides =2, activation='relu')
            self.conv3 = tf.keras.layers.Conv2D(64, kernel_size =2, strides =1, activation='relu')
            self.flat = tf.keras.layers.Flatten()
            self.d1 = tf.keras.layers.Dense(512, activation='relu')
            self.d2 = tf.keras.layers.Dense(128, activation='relu')
            self.fd =tf.keras.layers.Dense(self.class_size, activation='softmax') 

        def call(self, inputs):
            x = self.conv1(inputs)
            x = self.conv2(x)
            x = self.conv3(x)
            x = self.flat(x)
            x = self.d1(x)
            x = self.d2(x)
            return self.fd(x)

    model = Mymodel(10)

    train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
    train_images, test_images = train_images / 255.0, test_images / 255.0

    train_ds = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels)).shuffle(1000).batch(32)

    test_ds = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(32)

    # define the training and testing objects 

    loss_object = tf.keras.losses.SparseCategoricalCrossentropy()

    optimizer = tf.keras.optimizers.Adam()

    @tf.function
    def train_step(images, labels):
      with tf.GradientTape() as tape:
        predictions = model(images)
        loss = loss_object(labels, predictions)
      gradients = tape.gradient(loss, model.trainable_variables)
      optimizer.apply_gradients(zip(gradients, model.trainable_variables))
      loss(loss)
      accuracy(labels, predictions)


tf.function
def test_step(images, labels):
  predictions = model(images)
  t_loss = loss_object(labels, predictions)
  loss(t_loss)
  accuracy(labels, predictions)


def train():
    EPOCHS = 5

    for epoch in range(EPOCHS):
      for images, labels in train_ds:
        train_step(images, labels)

      for test_images, test_labels in test_ds:
        test_step(test_images, test_labels)

      template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
      print(template.format(epoch+1,
                        train_loss.result(),
                        train_accuracy.result()*100,
                        test_loss.result(),
                        test_accuracy.result()*100))

      # Reset the metrics for the next epoch
    train_loss.reset_states()
    train_accuracy.reset_states()
    test_loss.reset_states()
    test_accuracy.reset_states()
train()

当我替换 compile 和 fit 函数时它可以工作。

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_images, train_labels, batch_size= 200, epochs=6, validation_data=(test_images, test_labels))

非常适合任何帮助。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    在损失函数中设置 from_logits=True。

    loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

    它解决了错误!

    【讨论】:

      【解决方案2】:

      我认为你可以先使用 input_shape 参数。

      self.conv1 = tf.keras.layers.Conv2D(32, kernel_size =3, strides =2, activation='relu', input_shape=(w,h,n_channel)

      【讨论】:

        猜你喜欢
        • 2017-10-24
        • 1970-01-01
        • 2016-09-29
        • 2023-04-03
        • 2020-10-28
        • 2018-12-07
        • 1970-01-01
        • 2020-07-13
        • 2020-10-19
        相关资源
        最近更新 更多