deep-hedging icon indicating copy to clipboard operation
deep-hedging copied to clipboard

Error in Google Colab Notebook

Open armarion opened this issue 3 years ago • 4 comments

Hello

I am encountering the following error when attempting to run the notebook in Google Colab



#@title <font color='Blue'>**Run the Deep Hedging Algorithm (Simple Network)!**</font>
%autoreload 2

optimizer = Adam(learning_rate=lr)

# Setup and compile the model
model_simple = Deep_Hedging_Model(N=N, d=d+2, m=m, risk_free=risk_free, \
          dt = dt, strategy_type="simple", epsilon = epsilon, \
          use_batch_norm = use_batch_norm, kernel_initializer = kernel_initializer, \
          activation_dense = activation_dense, activation_output = activation_output, \
          final_period_cost = final_period_cost, delta_constraint = delta_constraint, \
          share_stretegy_across_time = share_stretegy_across_time, \
          cost_structure = cost_structure)
loss = Entropy(model_simple.output,None,loss_param)
model_simple.add_loss(loss)

model_simple.compile(optimizer=optimizer)

early_stopping = EarlyStopping(monitor="loss", \
          patience=10, min_delta=1e-4, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor="loss", \
          factor=0.5, patience=2, min_delta=1e-3, verbose=0)

callbacks = [early_stopping, reduce_lr]

# Fit the model.
model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs, \
          validation_data=xtest, verbose=1)

clear_output()

print("Finished running deep hedging algorithm! (Simple Network)")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-93bd618dd0f0> in <module>()
     17 
     18 # Fit the model.
---> 19 model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs,           validation_data=xtest, verbose=1)
     20 
     21 clear_output()

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py in unpack_x_y_sample_weight(data)
   1579     error_msg = ("Data is expected to be in format `x`, `(x,)`, `(x, y)`, "
   1580                  "or `(x, y, sample_weight)`, found: {}").format(data)
-> 1581     raise ValueError(error_msg)
   1582 
   1583 

ValueError: Data is expected to be in format `x`, `(x,)`, `(x, y)`, or `(x, y, sample_weight)`, found: (array([[100.

armarion avatar Dec 14 '21 06:12 armarion

bump

mvind avatar Feb 08 '22 13:02 mvind

A quick fix instead of passing explicit validation data, you can just increase the xtrain dataset and use the input as validation data using validation_split like so:

model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs, \
          validation_split=0.1, verbose=1)

mvind avatar Feb 08 '22 13:02 mvind

Yes, thank you for the quick fix. It is possible that there's some changes in the tensorflow version in the Colab so the data shape is no longer valid. The error is related to the shape of the data (i.e. x or (x,)), but unfortunately I do not yet have time to update the code.

YuMan-Tam avatar Feb 21 '22 03:02 YuMan-Tam

I could get it to run at least when adding brackets when providing xtest as the validation data. I'm not sure if this is necessarily valid, but it does appear to generate output numbers and graphs which are substantially similar to the originals.

# Fit the model. model_simple.fit(x=xtrain, batch_size=batch_size, epochs=epochs, \ validation_data=[xtest], verbose=1)

Hawaiiwong avatar Sep 19 '23 18:09 Hawaiiwong