here is my code and written in keras
import keras
from keras import layers
from keras import Sequential
from keras.layers import Dense, Flatten
import numpy as np
from keras.engine.topology import Input
from keras.engine.training import Model
class _Model:
def __init__(self,state,n_dims, n_action):
self.n_action=n_action
self.n_dims=n_dims
self.state=state
self.model=self.build_model()
def build_model(self):
inx=model=Input((10,16))
model=Flatten()(model)
model=Dense(512, activation=None)(model)
model=Dense(512, activation=None)(model)
p_model=Dense(self.n_action, activation='sigmoid')(model)
v_model=Dense(1, activation='tanh')(model)
_model=Model(inx,[p_model,v_model])
losses = ['categorical_crossentropy', 'mean_squared_error']
_model.compile(loss=losses, optimizer='adam')
print(_model.summary())
return _model
def predict(self,state):
return self.model.predict(state)
def train(self, state, action_probability, leaf_value):
batch_size=11
state=np.array(state)
action_probability=np.array(action_probability)
leaf_value=np.array(leaf_value)
self.model.fit(state, [action_probability, leaf_value],batch_size=batch_size,verbose=1)
loss=self.model.evaluate(state, [action_probability, leaf_value],batch_size=batch_size,verbose=0)
return loss[0]
state=[ 4321432141243124,
1423123424143213,
4321432143213421,
4321431241324323,
1243121234214334,
4123123421342314,
4321432434212412,
4121432121121343,
4123413412412321,
4123413412413431,
]
m=_Model(state,16,4)
m.train(state,[0.1,0.5,0.4,0.7],0.4)
This implementation is for alphago zero. I'm trying to implement the model that has two outputs. The two values (P, v) that p is action probability and v is winning probability. what's the problem? what is matter with this code? The error says the list is out of index but I don't know what list. Should I change the input_shape?
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-9-c5b4b76d5be5> in <module>()
48 ]
49 m=_Model(state,16,4)
---> 50 m.train(state,[0.1,0.5,0.4,0.7],0.4)
6 frames
<ipython-input-9-c5b4b76d5be5> in train(self, state, action_probability, leaf_value)
32 action_probability=np.array(action_probability)
33 leaf_value=np.array(leaf_value)
---> 34 self.model.fit(state, [action_probability, leaf_value],batch_size=batch_size,verbose=1)
35 loss=self.model.evaluate(state, [action_probability, leaf_value],batch_size=batch_size,verbose=0)
36 return loss[0]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/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, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1061 use_multiprocessing=use_multiprocessing,
1062 model=self,
-> 1063 steps_per_execution=self._steps_per_execution)
1064
1065 # Container that configures and calls `tf.keras.Callback`s.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1115 use_multiprocessing=use_multiprocessing,
1116 distribution_strategy=ds_context.get_strategy(),
-> 1117 model=model)
1118
1119 strategy = ds_context.get_strategy()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
273 inputs = pack_x_y_sample_weight(x, y, sample_weights)
274
--> 275 num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
276 if len(num_samples) > 1:
277 msg = "Data cardinality is ambiguous:\n"
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in <genexpr>(.0)
273 inputs = pack_x_y_sample_weight(x, y, sample_weights)
274
--> 275 num_samples = set(int(i.shape[0]) for i in nest.flatten(inputs))
276 if len(num_samples) > 1:
277 msg = "Data cardinality is ambiguous:\n"
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
885 else:
886 if self._v2_behavior:
--> 887 return self._dims[key].value
888 else:
889 return self._dims[key]
IndexError: list index out of range
you are giving 1D array to keras fit method, keras need all inputs and outputs in atleast 2D. try this.
state = np.array(state)
state = np.reshape(state, (-1,state.shape[0]))
action_probability = np.array(action_probability)
action_probability = np.reshape(action_probability, (-1,action_probability.shape[0]))
leaf_value = np.array(leaf_value)
leaf_value = np.reshape(leaf_value, (-1,leaf_value.shape[0]))