为 CNN 导入 Keras
Importing Keras for a CNN
由于添加了层 BatchNormalization,我得到了一个 TypeErrir,它与 class 层不同。我不确定为什么,我尝试正确导入图层,并尝试了多种不同的方法。
我目前的导入是:
import copy
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization,Dense, Conv2D, Flatten, Reshape
from tensorflow.keras.layers import Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Input
我在以下代码部分使用导入:
model = Sequential()
model.add(Input(shape=(9, 9, 1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization)
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization)
model.add(Conv2D(128, kernel_size=(1, 1), activation='relu', padding='same'))
model.add(Flatten())
model.add(Dense(81 * 9))
model.add(Reshape((-1, 9)))
model.add(Activation('softmax'))
adam = Adam(lr=.001)
model.compile(loss='sparse_categorical_crossentropy', optimizer=adam)
model.fit(x_train, y_train, batch_size=32, epochs=2)
我得到的错误是:
File "**/train.py", line 24, in <module>
x_train, x_test, y_train, y_test = get_data('sudoku.csv')
File "**/data_preprocess.py", line 124, in get_data
model.add(BatchNormalization)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py", line 180, in add
raise TypeError('The added layer must be '
TypeError: The added layer must be an instance of class Layer. Found: <class 'tensorflow.python.keras.layers.normalization_v2.BatchNormalization'>
我也尝试了以下方法,但遇到了同样的错误。
错误是否与项目中的其他内容有关?除了进口
你快到了。 Batchnorm 是一个 class 所以你需要通过添加 ()
来实例化它
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization())
由于添加了层 BatchNormalization,我得到了一个 TypeErrir,它与 class 层不同。我不确定为什么,我尝试正确导入图层,并尝试了多种不同的方法。
我目前的导入是:
import copy
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization,Dense, Conv2D, Flatten, Reshape
from tensorflow.keras.layers import Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Input
我在以下代码部分使用导入:
model = Sequential()
model.add(Input(shape=(9, 9, 1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization)
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization)
model.add(Conv2D(128, kernel_size=(1, 1), activation='relu', padding='same'))
model.add(Flatten())
model.add(Dense(81 * 9))
model.add(Reshape((-1, 9)))
model.add(Activation('softmax'))
adam = Adam(lr=.001)
model.compile(loss='sparse_categorical_crossentropy', optimizer=adam)
model.fit(x_train, y_train, batch_size=32, epochs=2)
我得到的错误是:
File "**/train.py", line 24, in <module>
x_train, x_test, y_train, y_test = get_data('sudoku.csv')
File "**/data_preprocess.py", line 124, in get_data
model.add(BatchNormalization)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py", line 180, in add
raise TypeError('The added layer must be '
TypeError: The added layer must be an instance of class Layer. Found: <class 'tensorflow.python.keras.layers.normalization_v2.BatchNormalization'>
我也尝试了以下方法,但遇到了同样的错误。
错误是否与项目中的其他内容有关?除了进口
你快到了。 Batchnorm 是一个 class 所以你需要通过添加 ()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization())