如何在Keras的图层中设置一些权重

时间:2018-04-28 19:54:13

标签: python 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) 

1 个答案:

答案 0 :(得分:0)

好的,问题在于使用权重作为变量类型,而不是使用里面的值。通过在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)