Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将keras顺序API转换为函数API_Python_Machine Learning_Keras_Deep Learning_Image Recognition - Fatal编程技术网

Python 如何将keras顺序API转换为函数API

Python 如何将keras顺序API转换为函数API,python,machine-learning,keras,deep-learning,image-recognition,Python,Machine Learning,Keras,Deep Learning,Image Recognition,我是深度学习新手,正在尝试将此顺序API转换为功能API,以便在CIFAR 10数据集上运行。以下是顺序API: model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activatio

我是深度学习新手,正在尝试将此顺序API转换为功能API,以便在CIFAR 10数据集上运行。以下是顺序API:

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu')

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
下面是我将其转换为函数API的尝试:

model_input = Input(shape=input_shape)

x = Conv2D(32, (3, 3), activation='relu',padding='valid')(model_input)
x = MaxPooling2D((2,2))(x)
x = Conv2D(32, (3, 3), activation='relu')(x)
x = MaxPooling2D((2,2))(x)
x = Conv2D(32, (3, 3))(x)

x = GlobalAveragePooling2D()(x)
x = Activation(activation='softmax')(x)

model = Model(model_input, x, name='nin_cnn')

x = layers.Flatten()
x = layers.Dense(64, activation='relu')
x = layers.Dense(10)
以下是编译和训练代码:

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))

原始顺序API的精度为0.7175999879837036,而函数API的精度为0.05029983727932。不知道我在重新编写代码时哪里出错了,如果有任何帮助,我们将不胜感激。谢谢。

你们的两种型号不一样。第二和第三卷积层在示例代码中分别有64个单元和32个单元用于序列和函数模型。并且在功能模型中没有包含完全连接的层(只有在构建模型后才创建这些层)

如果你将来怀疑,你可以试着去做

model.summary()

然后比较两种型号是否相同。

您的两种型号不一样。第二和第三卷积层在示例代码中分别有64个单元和32个单元用于序列和函数模型。并且在功能模型中没有包含完全连接的层(只有在构建模型后才创建这些层)

如果你将来怀疑,你可以试着去做

model.summary()

比较一下,看看模型是否相同。

除了@adrtam提到的内容,我还想添加一些内容,因为用户是初学者

顺序模型和函数模型之间有两个重要的区别。功能和顺序几乎相似,除了

因此,顺序是单输入单输出,可以逐层添加/堆叠层。功能性定制更具灵活性。所以,在某种程度上,我们可以说序贯是函数模型的子集

来到您的案例中,
这是一个顺序模型

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import models
input_shape=(32, 32, 3)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
这是功能模型

from tensorflow.keras import Model
from tensorflow.keras import layers
model_input = layers.Input(shape=input_shape)
x = layers.Conv2D(32, (3, 3), activation='relu',padding='valid')(model_input)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dense(10)(x)

model2 = Model(model_input, x, name='nin_cnn')

除了@adrtam提到的内容之外,我还想添加一些内容,因为用户是初学者

顺序模型和函数模型之间有两个重要的区别。功能和顺序几乎相似,除了

因此,顺序是单输入单输出,可以逐层添加/堆叠层。功能性定制更具灵活性。所以,在某种程度上,我们可以说序贯是函数模型的子集

来到您的案例中,
这是一个顺序模型

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import models
input_shape=(32, 32, 3)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
这是功能模型

from tensorflow.keras import Model
from tensorflow.keras import layers
model_input = layers.Input(shape=input_shape)
x = layers.Conv2D(32, (3, 3), activation='relu',padding='valid')(model_input)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.MaxPooling2D((2,2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dense(10)(x)

model2 = Model(model_input, x, name='nin_cnn')

使用此函数模型后,程序仍能获得约10%的精度。我不确定哪里出了问题。如果你想看的话,我可以发布整个代码?干杯。使用这个函数模型后,程序的准确率仍然在10%左右。我不确定哪里出了问题。如果你想看的话,我可以发布整个代码?干杯