0
$\begingroup$

I want to learn a policy network for a Domineering game

  • For each position I have to recall the input (i.e. the board, the flipped board, and the turn) and the output of the same size as the board with only the best move found by the Monte Carlo evaluation marked as 1.

  • For instance csv lines for a 2x2 board :

    0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0

which corresponds to board, flipped board, player plane, move to learn

Corresponding input tensor :

0 0 1 1 1 1
0 0 1 1 1 1

Corresponding output tensor :

1 0
0 0

From here I have a 8*8 board games database. With this tutorial I already developed a failing neural network. Indeed I did 12 nodes for 128 inputs there seems to be a problem with the first layer model.add(Dense(12, input_dim=128, activation='relu')).

# model construction

model = Sequential()
model.add(Dense(12, input_dim=128, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='sigmoid'))

print("compile")
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print("fit")
model.fit(X_test, Y_test, epochs=3, batch_size=10)

In effect it gives me the following error value:

ValueError: Error when checking target: expected dense_27 to have shape (128,) but got array with shape (127,)

And when I try to replace with 127, just to see, it says:

ValueError: Error when checking input: expected dense_28_input to have shape (127,) but got array with shape (128,)

Here is the entire code, which you can get on GitHub as well. It's the ipython notebook.

#!/usr/bin/env python3
from timeit import default_timer as timer

import csv 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")

from keras.models import Sequential
from keras.layers import Dense

# we divide data.csv into train and tests 

with open("data.csv", 'r') as f:
    plays = np.array(list(csv.reader(f, delimiter=",")))
    print(plays.shape)    
# We take the 126 first columns as input
df = pd.DataFrame(data=plays[0:28961,1:256])
# We take the 126 last columns as output
Y = pd.DataFrame(data=plays[0:28961,129:256])

#plays.reshape((64,64))

#board = np.reshape(plays, (8, 8))

df['split'] = np.random.randn(df.shape[0], 1)
msk = np.random.rand(len(df)) <= 0.7

train_df = df[msk].fillna("sterby")
test_df = df[~msk].fillna("sterby")

# we take the 128 first columns has input
X_train = train_df.iloc[:,0:128].values
# we take the 128 last columns has input
y_train = train_df.iloc[:,129:].values
X_test = test_df.iloc[:,0:128].values
Y_test = test_df.iloc[:,129:].values

# Necesary Keras Importations

from keras.preprocessing import sequence
from keras.models import Model, Input
from keras.layers import Dense, Embedding, GlobalMaxPooling1D
from keras.preprocessing.text import Tokenizer
from keras.optimizers import Adam

# model construction

model = Sequential()
model.add(Dense(12, input_dim=128, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='sigmoid'))

print("compile")
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print("fit")
model.fit(X_test, Y_test, epochs=3, batch_size=10)

print("evaluate")

# evaluate the model
scores = model.evaluate(X_test, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

And the error :

compile 
fit
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-8b0dc569bd70> in <module>()
      3 
      4 print("fit\n")
----> 5 model.fit(X_test, Y_test, epochs=3, batch_size=10)
      6 
      7 # evaluate the model

/usr/local/lib/python3.5/dist-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    961                               initial_epoch=initial_epoch,
    962                               steps_per_epoch=steps_per_epoch,
--> 963                               validation_steps=validation_steps)
    964 
    965     def evaluate(self, x=None, y=None,

/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1628             sample_weight=sample_weight,
   1629             class_weight=class_weight,
-> 1630             batch_size=batch_size)
   1631         # Prepare validation data.
   1632         do_validation = False

/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
   1478                                     output_shapes,
   1479                                     check_batch_axis=False,
-> 1480                                     exception_prefix='target')
   1481         sample_weights = _standardize_sample_weights(sample_weight,
   1482                                                      self._feed_output_names)

/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    121                             ': expected ' + names[i] + ' to have shape ' +
    122                             str(shape) + ' but got array with shape ' +
--> 123                             str(data_shape))
    124     return data
    125 

ValueError: Error when checking target: expected dense_12 to have shape (128,) but got array with shape (127,)
$\endgroup$
  • $\begingroup$ Welcome to ai.se...If you found the answer useful you can accept the answer $\endgroup$ – DuttaA Mar 14 '18 at 9:29
1
$\begingroup$

Python indexes start with 0 and do not take the last element of the range. So the indexes 129:256 have only 127 elements. You should start with zero. Then 0:128 has 128 elements and 128:256 has 128 elements.

The technical word for this is slice indexing.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.