title: set weights of keras layers
body: <p>I'm trying to do the game Snake using genetic algorithm and Keras.</p>
  
  <p>My problem, right now, is the following:</p>
  
  <p>I create the initial population of snake with X genes each, where X is NUMBER_WEIGHTS: </p>
  
  <pre><code>INPUT = 24
  NEURONS_HIDDEN_1 = 16
  NEURONS_HIDDEN_2 = 16
  OUTPUT = 3
  NUMBER_WEIGHTS = INPUT * NEURONS_HIDDEN_1 + NEURONS_HIDDEN_1 * NEURONS_HIDDEN_2 + NEURONS_HIDDEN_2 * OUTPUT
  </code></pre>
  
  <p>and i create the initial population like this:</p>
  
  <pre><code>population = numpy.random.choice(numpy.arange(-1, 1, step=0.01), size=(config.NUMBER_OF_POPULATION, config.NUMBER_WEIGHTS))
  </code></pre>
  
  <p>I have a for cycle that for each snake inside my population start the pygame script, inside the pygame script i have the Keras NN, but i would like to pass the weights that i have generated to the NN.</p>
  
  <p>My NN is like that for now:</p>
  
  <pre><code>from keras.layers import Dense, Activation
  from keras.models import Sequential
  from keras.optimizers import SGD
  
  from utils import config
  def neural_net(weights):
      model = Sequential()
  
      model.add(Dense(config.INPUT, input_shape=(config.INPUT,)))
      model.add(Activation('relu'))
      # create the dense input layer
      # model.add(Dense(config.INPUT, activation=keras.activations.relu(4,), input_dim=4))
      # model.add(Activation('sigmoid'))
  
      # create first hidden layer
      model.add(Dense(config.NEURONS_HIDDEN_1, input_shape=(config.INPUT,)))
      model.add(Activation('relu'))
      # create second hidden layer
      model.add(Dense(config.NEURONS_HIDDEN_2, input_shape=(config.NEURONS_HIDDEN_1,)))
      model.add(Activation('relu'))
      # create output layer
      model.add(Dense(config.OUTPUT, input_shape=(config.NEURONS_HIDDEN_2,)))
      model.add(Activation('softmax'))
  
      print(weights.shape[0])
      model.set_weights(weights)
  
      # create the optimizer (Stochastic Gradient Descent)
      sgd = SGD(lr=0.01, decay=0.0, momentum=0.0, nesterov=False)
      # Use mean squared error loss and SGD as optimizer
      model.compile(loss='mse', optimizer=sgd)
  
      return model
  </code></pre>
  
  <p>But model.set_weights(weights) return this exception: </p>
  
  <pre><code>File "neural_network.py", line 28, in neural_net
      model.set_weights(weights)
    File "C:\Users\Davide\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\network.py", line 527, in set_weights
      K.batch_set_value(tuples)
    File "C:\Users\Davide\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\backend\tensorflow_backend.py", line 2960, in batch_set_value
      tf_keras_backend.batch_set_value(tuples)
    File "C:\Users\Davide\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3323, in batch_set_value
      x.assign(np.asarray(value, dtype=dtype(x)))
    File "C:\Users\Davide\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py", line 819, in assign
      self._shape.assert_is_compatible_with(value_tensor.shape)
    File "C:\Users\Davide\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\tensor_shape.py", line 1110, in assert_is_compatible_with
      raise ValueError("Shapes %s and %s are incompatible" % (self, other))
  ValueError: Shapes (24, 24) and () are incompatible
  
  Process finished with exit code 1
  </code></pre>
  
  <p>We have 24 input * 16 neuros on the hidden layer one, then 16 neurons * 16 neurons on layer hidden 2 and finally 16 neurons hidden layer 2 * 3 inputs = 24*16+16*16+16*3 = 688</p>
  
  <p>And </p>
  
  <pre><code>print(weights.shape[0])
  </code></pre>
  
  <p>is 688. So why i can't set the correct weights?</p>
  
  <p>First time doing a project using AI, so i may have completely misunderstood how this works</p>