title: How to set some weights in a layer in Keras
body: <p>Is there a way to set particular weights to be a specific value in Keras layers?</p>
  
  <p>For example, I have a set of 3x3 numpy arrays as input. They are arranged in sequences of 7. Each array has values 0, 1, or -1. For positions with the value -1, I want there to be no contribution to weight calculations or the loss function. I had thought <code>Masking</code> would provide what I was looking for, but that proved to be a dead end: you cannot mask individual values within an input example, as far as I can tell. </p>
  
  <p>Is there a way to use <code>set_weights</code> to accomplish this?</p>
  
  <p>This code is what I have so far (no <code>set_weights</code> yet).</p>
  
  <pre><code>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 &lt; 0.2] = 0.
  X[(X &gt;= 0.2) &amp; (X &lt; 0.4)] = 1.
  X[X &gt;= 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')
  </code></pre>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Now with 500 training examples, slightly modified the model for dimension purposes (now without return_sequences=True - I needed that when I was experimenting with <code>Masking</code>, but now it's not necessary). Please keep in mind these data are random, so we're not expecting a good fit here. </p>
  
  <pre><code>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 &lt; 0.2] = 0.
  X[(X &gt;= 0.2) &amp; (X &lt; 0.4)] = 1.
  X[X &gt;= 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) 
  </code></pre>