[英]How to set some weights in a layer in Keras
是否可以在Keras圖層中將特定權重設置為特定值?
例如,我有一組3x3的numpy數組作為輸入。 它們以7的順序排列。每個數組的值為0、1或-1。 對於值為-1的頭寸,我希望對重量計算或損失函數沒有任何貢獻。 我以為Masking
可以提供我想要的東西,但是事實證明這是一個死胡同:據我所知,您不能屏蔽輸入示例中的各個值。
有沒有辦法使用set_weights
完成此操作?
這段代碼是我到目前為止(沒有set_weights
)。
from keras.layers import Input, LSTM, Dense
from keras.models import Model
import numpy as np
from keras.optimizers import adam
#Creating some sample data
#Matrix has size 3*3, values -1, 0, 1
X = np.random.rand(3, 3).flatten()
X[X < 0.2] = 0.
X[(X >= 0.2) & (X < 0.4)] = 1.
X[X >= 0.4] = -1
X2 = np.random.rand(7, 3*3)
for i in range(X2.shape[0]):
X2[i,:][(X==-1.)] = -1.
X2[i,:][(X !=-1.)] = 0.
tobeone = int(len(np.where(X2[i,:] == 0.)[0])*0.5)
selected_ones = np.random.choice(np.where(X2[i,:] == 0.)[0], tobeone)
X2[i,selected_ones] = 1.
X = np.reshape(X, ((1, 3*3)))
X_new = np.concatenate((X, X2), axis=0)
y_true = X_new[7,:]
X = X_new[:7,:]
#Building the model
input_tensor = Input(shape=(7, 3*3))
lstm = LSTM(1, return_sequences=True)(input_tensor)
output = Dense(3*3, activation='sigmoid')(lstm)
model = Model(input_tensor, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
編輯
現在有500個訓練示例,出於尺寸目的對模型進行了稍微修改(現在沒有return_sequences = True-在嘗試Masking
時我需要它,但現在沒有必要了)。 請記住,這些數據是隨機的,因此在這里我們不希望有合適的數據。
from keras import backend as K
from keras.utils import to_categorical
from keras.optimizers import adam
import sys
#Creating some sample data
#Matrix has size 3*3, values -1, 0, 1
X = np.random.rand(7, 3, 3).flatten() #7*3*3 = 42
X[X < 0.2] = 0.
X[(X >= 0.2) & (X < 0.4)] = 1.
X[X >= 0.4] = -1
Xlist = list()
Xlist.append(X)
for j in range(499): #500 total input examples
X2 = np.random.rand(7, 3, 3).flatten()
X2[(X==-1.)] = -1.
X2[(X !=-1.)] = 0.
tobeone = int(len(np.where(X2 == 0.)[0])*0.5)
selected_ones = np.random.choice(np.where(X2 == 0.)[0], tobeone)
X2[selected_ones] = 1.
X2 = np.reshape(X2, ((7, 3, 3)))
Xlist.append(X2)
Xlist[0] = np.reshape(Xlist[0], ((7, 3, 3)))
X = np.asarray(Xlist)
X = np.reshape(X, ((500, 7, 3*3)))
Y = X[:, -1, :]
y_true = Y
X = X[:, :-1, :]
#print(y_true.shape, X.shape) #(500, 9) and (500, 6, 9)
#Building the model
input_tensor = Input(shape=(X.shape[1], X.shape[2]))
lstm = LSTM(1)(input_tensor) #return_sequences=True)(input_tensor)
output = Dense(X.shape[2], activation='sigmoid')(lstm)
model = Model(input_tensor, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
print(model.summary())
model.fit(X, y_true, batch_size = 10, epochs = 10, verbose=2)
好的,問題在於使用權重作為變量類型,而不是使用內部的值。 通過在custom_reg
設置weights = K.eval(weights)
custom_reg
。
from keras.layers import Input, LSTM, Dense
from keras.models import Model
from keras import backend as K
from tensorflow.python.ops.variables import Variable
import numpy as np
import keras
from keras.optimizers import adam
from keras.callbacks import LambdaCallback
class SaveWeightsandRegularize(keras.callbacks.Callback):
# Batch_num is defined so we can access the current X input into the model, as well as save layer params per batch.
batch_num = 0
X = None # We'll set this to our X data
def on_train_begin(self, logs={}):
self.weights = {}
def on_batch_end(self, batch, logs={}):
self.batch_num += 1
for layer in model.layers:
self.weights[str(self.batch_num)] = layer.get_weights()
def custom_reg(self, weights):
# We set the already computed weights where the value of the input is -1 to a value of 0.
weights = K.eval(weights)
loss_contrib = np.where(X[self.batch_num] > -1, weights, 0)
return Variable(loss_contrib)
# Creating some sample data
def gen_data():
# Matrix has size 3*3, values -1, 0, 1
X = np.random.rand(7, 3, 3).flatten() # 7*3*3 = 42
X[X < 0.2] = 0.
X[(X >= 0.2) & (X < 0.4)] = 1.
X[X >= 0.4] = -1
Xlist = list()
Xlist.append(X)
for j in range(499): # 500 total input examples
X2 = np.random.rand(7, 3, 3).flatten()
X2[(X==-1.)] = -1.
X2[(X !=-1.)] = 0.
tobeone = int(len(np.where(X2 == 0.)[0])*0.5)
selected_ones = np.random.choice(np.where(X2 == 0.)[0], tobeone)
X2[selected_ones] = 1.
X2 = np.reshape(X2, ((7, 3, 3)))
Xlist.append(X2)
Xlist[0] = np.reshape(Xlist[0], ((7, 3, 3)))
X = np.asarray(Xlist)
X = np.reshape(X, ((500, 7, 3*3)))
Y = X[:, -1, :]
y_true = Y
X = X[:, :-1, :]
return X, y_true
X, y_true = gen_data()
save_weights_regularize = SaveWeightsandRegularize()
save_weights_regularize.X = X
print("input shape", X.shape)
# Building the model
input_tensor = Input(shape=(X.shape[1], X.shape[2]))
lstm = LSTM(1)(input_tensor) # return_sequences=True)(input_tensor)
output = Dense(X.shape[2], activation='sigmoid', kernel_regularizer=save_weights_regularize.custom_reg)(lstm)
model = Model(input_tensor, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(X, y_true, batch_size=1, epochs=10, verbose=2, callbacks=[save_weights_regularize])
print(save_weights_regularize.weights)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.