我正在尝试将vgg16层添加到顺序模型中,但出现问题标题中提到的错误
stock = {
3AB: {'Name': 'Telcom', 'Purchase Date': '12/12/2018', 'Price': '1.55', 'Volume':'3000'},
S12: {'Name': 'S&P', 'Purchase Date': '12/08/2018', 'Price': '3.25', 'Volume': '2000'},
AE1: {'Name': 'A ENG', 'Purchase Date': '04/03/2018', 'Price': '1.45', 'Volume': '5000'}
}
print(stock[3AB]['Name'])
print(stock[S12]['Name'])
print(stock[AE1]['Name'])
我正在使用keras 2.2.4
from keras.applications.vgg16 import VGG16
from tensorflow.contrib.keras.api.keras.models import Sequential
vgg_model = VGG16()
model = Sequential()
#print(model.summary())
for layer in vgg_model.layers:
model.add(layer)
print(model.summary())
答案 0 :(得分:0)
假设您要删除最后一层,并添加自己的最后一个具有10个节点的完整连接层。要实现此功能,可以使用keras功能API。
from tensorflow.contrib.keras.api.keras.models import Sequential
import keras
from keras_applications.vgg16 import VGG16
vgg_model = VGG16()
# replace the last layer with new layer with 10 nodes.
last_layer = vgg_model.layers[-2].output ##
output = keras.layers.Dense(10, activation="softmax")(last_layer)
model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)
model.summary()
print(model.summary())
或使用include_top = False
vgg_model = VGG16(include_top=False)
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)
model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)
您可能要使用预先训练的重量。您可以通过使用weights参数来实现
vgg_model = VGG16(weights='imagenet',include_top=False)
您可能想冻结一些图层。
number_of_layers_to_freeze = 10
vgg_model = VGG16(include_top=False)
for i in range(number_of_layers_to_freeze):
vgg_model.layers[i].trainable = False
vgg_output = vgg_model.outputs[0]
output = keras.layers.Dense(10, activation="softmax")(vgg_output)
model = keras.models.Model(inputs=vgg_model.inputs, outputs=output)