【问题标题】:tf.keras.Sequential() Fails - Python Tensorflow Keras Error [closed]tf.keras.Sequential() 失败 - Python Tensorflow Keras 错误 [关闭]
【发布时间】:2022-01-16 06:34:45
【问题描述】:

我认为这个关于 cuda 的错误,实际上,我不确定。 有一个 google colab 链接,你可以从那里运行它。 谷歌 colab 链接:enter link description here

import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

(trainX, trainY), (testX, testY) = fashion_mnist.load_data()

# reshape dataset to have a single channel
trainX = trainX.reshape((trainX.shape[0], 28, 28, 1)) / 255.0
testX = testX.reshape((testX.shape[0], 28, 28, 1)) / 255.0
# one hot encode target values
trainY = to_categorical(trainY, 10)
testY = to_categorical(testY, 10)

def define_model():
# create the model
model = tf.keras.Sequential()
model.add(Conv2D(5, (3, 3), padding='same', activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2), strides = (1, 1)))
model.add(Conv2D(15, (5, 5), padding='valid', activation='sigmoid'))
model.add(Flatten())
model.add(Dense(100, activation='sigmoid'))
model.add(Dense(50, activation='relu'))
model.add(Dense(10))

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

for bs in [64, 128, 256]:
model = define_model()
model.fit(trainX, trainY, epochs=10, batch_size = 64)
_, acc = model.evaluate(testX, testY, verbose=0)
print('batch size: %d > %.3f' % (bs, acc * 100.0))

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    它说

    model = tf.keras.Sequential()
            ^
    IndentationError: expected an indented block
    

    您必须将方法def define_model() 的所有代码行向右移动一点。您的代码中完全没有缩进。在 for 循环中也是如此。 https://www.geeksforgeeks.org/indentation-in-python/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-16
      • 2021-03-24
      • 1970-01-01
      • 2021-08-26
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多