[英]Is it possible to set keras layer output?
I need to modify output of last feature map of my second convolution layer. 我需要修改第二个卷积层的最后一个特征图的输出。
Or add array to my conv layer output if it's possible. 或将数组添加到我的转换层输出中(如果可能)。
Below is python script i created and example of desired change in output. 以下是我创建的python脚本以及输出中所需更改的示例。
Thank you for your help! 谢谢您的帮助!
import numpy as np
from keras import backend as K
num=18
m=11
n=50
k=3
np.random.seed(100)
features = np.random.rand(num,m,n,k)
input_shape=features.shape[1:]
model = Sequential()
model.add(Conv2D(2, kernel_size=(1, 3), strides=(1,1),activation='relu',input_shape=input_shape))
model.add(Conv2D(21, kernel_size=(1, 48), strides=(1,1),padding="valid",activation='relu'))
model.add(Conv2D(1, kernel_size=(1, 1), strides=(1, 1),activation='relu',padding="valid"))
model.add(Dense(1, activation='softmax'))
Adam = optimizers.Adam(lr=0.00003, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='mse',optimizer=Adam)
get_1st_layer_output = K.function([model.layers[0].input],
[model.layers[1].output])
layer_output = get_1st_layer_output([features])
Setting DESIRED layer_output values 设置所需的layer_output值
I need to do it every propagation step. 我需要在每个传播步骤中进行操作。
for i in range(0,11):
layer_output[0][0][i][0][20]=0.1
print(layer_output[0][0][i][0][20])
I think I would use a concatenation with a constant tensor in that case. 我认为在这种情况下,我将使用具有恒定张量的串联。 Unfortunately, I can't quite get it to work, but I'll share my work anyway to hopefully help you on your way.
不幸的是,我无法完全正常工作,但无论如何我都会分享我的工作,希望对您有所帮助。
import numpy as np
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Conv2D, Dense, Concatenate
from keras import optimizers
num=18
m=11
n=50
k=3
np.random.seed(100)
features = np.random.rand(num, m, n, k)
custom_tensor = K.constant(0.1, shape=(11, 48, 1))
input_shape = features.shape[1:]
input = keras.Input(shape=input_shape)
print(K.ndim(input))
layer0 = Conv2D(2, kernel_size=(1, 3), strides=(1,1),activation='relu')(input)
layer0_added = Concatenate(axis=-1)([layer0, custom_tensor])
layer1 = Conv2D(20, kernel_size=(1, 48), strides=(1,1),padding="valid",activation='relu')(layer0_added)
layer2 = Conv2D(1, kernel_size=(1, 1), strides=(1, 1),activation='relu',padding="valid")(layer1)
layer3 = Dense(1, activation='softmax')(layer2)
model = keras.models.model(layer0)
Adam = optimizers.Adam(lr=0.00003, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='mse', optimizer=Adam)
It produced an error 它产生了一个错误
ValueError: `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 11, 48, 2), (11, 48, 1)]
But hopefully this helps you along anyway. 但希望这无论如何都能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.