如何添加调整大小图层
model = Sequential()
Run Code Online (Sandbox Code Playgroud)
运用
model.add(...)
Run Code Online (Sandbox Code Playgroud)
要将图像从形状(160,320,3)调整为(224,224,3)?
我在Keras使用顺序模型.我想在每个时代之后检查模型的重量.你能指导一下如何做到这一点.
model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=5 validation_data=(X_test, y_test))
Run Code Online (Sandbox Code Playgroud)
提前致谢.
嗨,当我尝试在keras中运行代码时,它显示以下错误:
from keras.utils.visualize_util import plot
ImportError: No module named 'keras.utils.visualize_util'
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?谢谢
我正在寻找一种在使用Keras创建的神经网络中获得变量重要性的正确或最佳方法.我目前这样做的方式是我只考虑第一层中变量的权重(而不是偏差),假设更重要的变量在第一层中具有更高的权重.有没有其他/更好的方法呢?
如何最好地将预处理层(例如,减去平均值并除以std)添加到keras(v2.0.5)模型,以使模型完全自包含以进行部署(可能在C++环境中).我试过了:
def getmodel():
model = Sequential()
mean_tensor = K.placeholder(shape=(1,1,3), name="mean_tensor")
std_tensor = K.placeholder(shape=(1,1,3), name="std_tensor")
preproc_layer = Lambda(lambda x: (x - mean_tensor) / (std_tensor + K.epsilon()),
input_shape=im_shape)
model.add(preproc_layer)
# Build the remaining model, perhaps set weights,
...
return model
Run Code Online (Sandbox Code Playgroud)
然后,在其他地方设置模型的均值/标准.我找到了set_value函数,所以尝试了以下方法:
m = getmodel()
mean, std = get_mean_std(..)
graph = K.get_session().graph
mean_tensor = graph.get_tensor_by_name("mean_tensor:0")
std_tensor = graph.get_tensor_by_name("std_tensor:0")
K.set_value(mean_tensor, mean)
K.set_value(std_tensor, std)
Run Code Online (Sandbox Code Playgroud)
然而set_value失败了
AttributeError: 'Tensor' object has no attribute 'assign'
Run Code Online (Sandbox Code Playgroud)
所以set_value不适用(有限的)文档建议.这样做的正确方法是什么?获取TF会话,将所有训练代码包装在一个with (session)并使用feed_dict?我原本以为会有一种原生的keras方式来设置张量值.
我没有使用占位符,而是尝试使用K.variable或设置模型构造的均值/标准 …
我一直在尝试使用池化层在Keras中构建顺序模型tf.nn.fractional_max_pool.我知道我可以尝试在Keras中创建自己的自定义图层,但我正在尝试查看是否可以在Tensorflow中使用该图层.对于以下代码段:
p_ratio=[1.0, 1.44, 1.44, 1.0]
model = Sequential()
model.add(ZeroPadding2D((2,2), input_shape=(1, 48, 48)))
model.add(Conv2D(320, (3, 3), activation=PReLU()))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(320, (3, 3), activation=PReLU()))
model.add(InputLayer(input_tensor=tf.nn.fractional_max_pool(model.layers[3].output, p_ratio)))
Run Code Online (Sandbox Code Playgroud)
我收到这个错误.我尝试了其他一些东西Input而不是InputLayerKeras Functional API,但到目前为止还没有运气.
我刚刚开始玩Keras并开始制作自定义图层.但是,我对许多不同类型的图层感到困惑,这些图层的名称略有不同,但具有相同的功能.
例如,https://keras.io/layers/merge/和https://www.tensorflow.org/api_docs/python/tf/keras/backend/concatenate有三种不同形式的连接函数.
keras.layers.Concatenate(axis=-1)
keras.layers.concatenate(inputs, axis=-1)
tf.keras.backend.concatenate()
Run Code Online (Sandbox Code Playgroud)
我知道第二个用于功能API,但3之间有什么区别?文档似乎有点不清楚.
此外,对于第三个,我看到了一个代码,在下面执行此操作.连接后为什么必须有._keras_shape行?
# Concatenate the summed atom and bond features
atoms_bonds_features = K.concatenate([atoms, summed_bond_features], axis=-1)
# Compute fingerprint
atoms_bonds_features._keras_shape = (None, max_atoms, num_atom_features + num_bond_features)
Run Code Online (Sandbox Code Playgroud)
最后,在keras.layers下,似乎总共有2个重复.例如,Add()和add()等.
我有一个正常的VGG16模型,有relu激活,即
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1, 1),input_shape=(3, 224, 224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
[...]
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
Run Code Online (Sandbox Code Playgroud)
我用现有的权重实例化它,现在想要将所有relu激活更改为softmax(没有用,我知道)
model = VGG_16('vgg16_weights.h5')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
softmax_act = keras.activations.softmax
for (n, layer) in enumerate(model.layers):
if 'activation' in layer.get_config() and layer.get_config()['activation'] == 'relu':
print('replacing #{}: {}, {}'.format(n, layer, layer.activation))
layer.activation = softmax_act …Run Code Online (Sandbox Code Playgroud) 我正在尝试Lambda在Keras中编写一个调用函数的图层,该图层connection运行一个循环for i in range(0,k),其中k作为函数的输入被输入connection(x,k).现在,当我尝试在Functional API中调用该函数时,我尝试使用:
k = 5
y = Lambda(connection)(x)
Run Code Online (Sandbox Code Playgroud)
也,
y = Lambda(connection)(x,k)
Run Code Online (Sandbox Code Playgroud)
但这些方法都没有奏效.如何在k不将其指定为全局参数的情况下输入值?
我想将ConvLSTM和Conv2D的输出传递给Keras中的Dense Layer,使用全局平均池和flatten之间的区别是两者都适用于我的情况.
model.add(ConvLSTM2D(filters=256,kernel_size=(3,3)))
model.add(Flatten())
# or model.add(GlobalAveragePooling2D())
model.add(Dense(256,activation='relu'))
Run Code Online (Sandbox Code Playgroud)