这篇文章主要是基于一下网站整理得到,提供一个整体的计算流程.关于实现细节,还是要重点看这个网站的内容,以及实例代码.
参考网站
输入端
from keras.layers import Input, Dense
from keras.models import Model
inputs = Input(shape=(784,))#这里输入的是不确定量
正向传播网络层
x = Dense(64, activation='relu')(inputs)
predictions = Dense(10, activation='softmax')(x)
Conv2D(64, (1, 1), padding='same', activation='relu')(input_img)
concatenated = keras.layers.concatenate([out_a, out_b])#合并
Flatten()#合并层
keras.layers.pooling.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)#池化层
#计算网络层的概念
layer.get_input_at(node_index)
layer.get_output_at(node_index)
layer.get_input_shape_at(node_index)
layer.get_output_shape_at(node_index)
反向传播层
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
compile(self, optimizer, loss, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)
model.fit(x_train, y_train, epochs=5, batch_size=32)
model.train_on_batch(x_batch, y_batch)#一个一个独立的算
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)#评估
classes = model.predict(x_test, batch_size=128)#预测
模型总体观察
model.summary()#查看模型情况
config = model.get_config()
model = Model.from_config(config)#重构配置
#保存网络结构
from models import model_from_json
json_string = model.to_json()
model = model_from_json(json_string)
yaml_string = model.to_yaml()
model = model_from_yaml(yaml_string)
#保存权重
model.save_weights(filepath)
model.load_weights(filepath, by_name=False)
如何建立一个自己的层(主要记住前三个主函数)
class BilinearUpsampling(Layer):
def __init__(self,upsampling=(2,2),**kwargs):
super(BilinearUpsampling,self).__init__(**kwargs)
self.input_spec=InputSpec(ndim=4)
self.upsampling=conv_utils.normalize_tuple(upsampling,2,'test1')
def compute_output_shape(self,input_shape):
height=self.upsampling[0]*input_shape[1]
width=self.upsampling[1]*input_shape[2]
return(input_shape[0],height,width,input_shape[3])
def call(self,inputs):
return K.tf.image.resize_bilinear(inputs,(inputs.shape[1]*self.upsampling[0],inputs.shape[2]*self.upsampling[1]))
def get_config(self):
config={'upsampling':self.upsampling,
'output_size':self.output_size,
'data_format':self.data_format}
base_config=super(BilinearUpsampling,self).get_config()
return dict(list(base_config.items())+list(config.items()))
网友评论