1

I am trying to build a network that predicts a curve such that the curve is close as possible to a group of curves together.

I defined this custom loss function

import keras.backend as K
def custom_loss(production_eval, ypred):
    return K.mean(K.mean(K.abs(production_eval-ypred),axis=1))

Here is the model itself

def BuilModel():

    maxlen = 12
    hidden_dims = 188
    l1_reg=0.002
    l2_reg=0.004
    std=0.005

    print('Build model...')
    main_input = Input(shape=(10*maxlen,1))

    ## split input into 10 (10 raw production curves)
    In = Lambda( lambda x: tf.split(x,num_or_size_splits=10,axis=1))(main_input)

    #Shared GRU
    shared_gru = Bidirectional(GRU(hidden_dims,activation='selu',
                               return_sequences=False,
                               kernel_regularizer=L1L2(l1=l1_reg, l2=l2_reg),
                               input_shape=(maxlen, 1)), name="Bi_GRU")
    x = concatenate([shared_gru(In[i]) for i in range(10)])
    x = Dense(hidden_dims,activation='relu')(x)
    main_output = Dense(12,activation='relu')(x)

    model = Model(inputs=main_input, outputs=main_output)

    model.compile(loss=custom_loss,outputs=main_output, optimizer='adam')   
    return model

I got the following error

/usr/local/lib/python3.6/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, init
ial_epoch, steps_per_epoch, validation_steps,
validation_freq, max_queue_size, workers, use_multiprocessing,
**kwargs)    1146         else:    1147             fit_inputs = x + y + sample_weights
-> 1148         self._make_train_function()    1149         fit_function = self.train_function    1150 

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in
_make_train_function(self)
    520                     updates=updates,
    521                     name='train_function',
--> 522                     **self._function_kwargs)
    523 
    524     def _make_test_function(self):

TypeError: function() got multiple values for argument 'outputs'

The first error is solved, however; I am getting this error in training

/usr/local/lib/python3.6/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, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1176                                         steps_per_epoch=steps_per_epoch,
   1177                                         validation_steps=validation_steps,
-> 1178                                         validation_freq=validation_freq)
   1179 
   1180     def evaluate(self,

/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    202                     ins_batch[i] = ins_batch[i].toarray()
    203 
--> 204                 outs = fit_function(ins_batch)
    205                 outs = to_list(outs)
    206                 for l, o in zip(out_labels, outs):

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2977                     return self._legacy_call(inputs)
   2978 
-> 2979             return self._call(inputs)
   2980         else:
   2981             if py_any(is_tensor(x) for x in inputs):

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
   2915                 array_vals.append(
   2916                     np.asarray(value,
-> 2917                                dtype=tf.as_dtype(tensor.dtype).as_numpy_dtype))
   2918         if self.feed_dict:
   2919             for key in sorted(self.feed_dict.keys()):

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    536 
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539 
    540 

ValueError: setting an array element with a sequence.

The training code is a 10 fold cross validation as follows

y_pred = []
y_true = []

kf = KFold(n_splits=10)
kf.get_n_splits(X)
for train_idx, test_idx in kf.split(X):
    Xtrain, Xtest = X[train_idx], X[test_idx]
    y_train, y_test = Ys[train_idx], Ys[test_idx]
    X_train = Xtrain.reshape(Xtrain.shape[0], Xtrain.shape[1], 1)
    X_test = Xtest.reshape(Xtest.shape[0], Xtest.shape[1],1)
    model = BuilModel()
    print(y_train.shape)

    model.fit(X_train, y_train,
          batch_size=64,
          nb_epoch=10,
          validation_data=(X_test, y_test))
    y_true.append(y_test)
    y_pred.append(model.predict(X_test))

Where X is is of shape (32000, 120) Y is an array of 2D matrices representing the targets

Muhammad
  • 61
  • 1
  • 6

1 Answers1

2

The outputs argument should not be present in model.compile.

Replace

model.compile(loss=custom_loss,outputs=main_output, optimizer='adam')

with

model.compile(loss=custom_loss, optimizer='adam')
Manoj Mohan
  • 5,654
  • 1
  • 17
  • 21
  • Thanks it solved this error, however; the training didn't work correctly it popped this error: ValueError: setting an array element with a sequence. – Muhammad Oct 01 '19 at 11:56
  • Please post the training code and stack trace, so that I can help. – Manoj Mohan Oct 01 '19 at 11:57
  • Just added to the original post – Muhammad Oct 01 '19 at 12:12
  • Is your y_train something like this since as_array call is failing? https://stackoverflow.com/a/4675383/5597718 – Manoj Mohan Oct 01 '19 at 12:50
  • Yes that's exactly my y_train shape (list of numpy(2d arrays)), each 2d array is of same column length and arbitrary number or rows, I tested the loss function separately on dummy data and it worked fine, but in the network it gave me the above error – Muhammad Oct 02 '19 at 07:21
  • Your Y should be of shape (32000, 12) and then split into y_train and y_test. If np.asarray(y_train) works, then everything should be fine. Not sure what you mean by arbitrary number of rows, maybe you can post a snippet of y_train. – Manoj Mohan Oct 02 '19 at 11:26