Reputation: 331
these codes were running flawlessly on tensorflow==2.15, for the purpose of GPU acceleration, I switched to tensorflow-gpu == 2.10.1 with keras 2.10, respectively. and this ValueError raised up on my screen when I try to use decay class for learning rates. These are my model build below:
#import packages
import pandas as pd
import numpy as np
import datetime as dt
import tushare as ts
import moduleigh as ml
token = ml.mytoken['Leigh']
ts.set_token(token)
pro = ts.pro_api(token)
# to plot within notebook
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#setting figure size
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10
rcParams['font.sans-serif'] = ['SimHei']
rcParams['axes.unicode_minus'] = False
import tensorflow as tf
import keras
from tensorflow.keras.layers import \
Input, BatchNormalization, GaussianNoise, \
Dense, Activation, Dropout, \
Concatenate, Layer, Conv1D, \
Bidirectional, LayerNormalization, LSTM, add
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam, schedules
from tensorflow.keras.losses import MeanSquaredError, BinaryCrossentropy
from tensorflow.keras.metrics import MeanAbsoluteError, AUC
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, TensorBoard
from tensorflow.keras.utils import plot_model
from tensorflow.compat.v1.keras.layers import CuDNNLSTM
# sklearn
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
### GPU
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
def cnn_lstm2_att():
model = build_model(**model_params)
lr = 0.001
ls = 0.01
batch = 16
epochs = 5
lr_scheduler = schedules.ExponentialDecay(
initial_learning_rate = lr,
decay_steps = int((x_train.shape[0] + batch)/batch),
decay_rate = 0.95
)
model.compile(
optimizer = Adam(learning_rate = lr_scheduler),
# optimizer = Adam(learning_rate = lr) ***this one works with a fix value***,
loss = {'dense_3': BinaryCrossentropy(label_smoothing = ls), },
metrics = {'dense_3': AUC(name = 'AUC'), },
)
my_callbacks = [
ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=10, min_lr=0.00001, verbose=1),
ModelCheckpoint(filepath=model_path, monitor='loss', save_best_only=True, verbose=1),
EarlyStopping(patience=10, monitor='loss', mode='min', verbose=1, restore_best_weights=True),
TensorBoard(log_dir=logdir, histogram_freq=1)
]
_history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch, verbose=1, callbacks=my_callbacks)
inputs = new_data[len(new_data) - len(valid_MLP) - 60:].values
inputs = inputs.reshape(-1,nums_features)
inputs = scaler.transform(inputs)
X_test = []
for i in range(60,inputs.shape[0]):
X_test.append(inputs[i-60:i,:])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], nums_features))
closing_price = model.predict(X_test)
closing_price_reshaped = np.zeros((closing_price.shape[0], nums_features))
closing_price_reshaped[:,0] = np.squeeze(closing_price)
preds = scaler.inverse_transform(closing_price_reshaped)[:,0]
rms=np.sqrt(np.mean(np.power((valid_MLP-preds),2)))
return preds, rms, _history
problem raised after first epoch, since the learning rate will decrease in the latter epochs:
---> 48 _history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch, verbose=1, callbacks=my_callbacks)
50 inputs = new_data[len(new_data) - len(valid_MLP) - 60:].values
51 inputs = inputs.reshape(-1,nums_features)
File ~\.conda\envs\py39tf27\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File ~\.conda\envs\py39tf27\lib\site-packages\tensorboard\plugins\scalar\summary_v2.py:88, in scalar(name, data, step, description)
83 summary_scope = (
84 getattr(tf.summary.experimental, "summary_scope", None)
85 or tf.summary.summary_scope
86 )
87 with summary_scope(name, "scalar_summary", values=[data, step]) as (tag, _):
---> 88 tf.debugging.assert_scalar(data)
89 return tf.summary.write(
90 tag=tag,
91 tensor=tf.cast(data, tf.float32),
92 step=step,
93 metadata=summary_metadata,
94 )
ValueError: Attempt to convert a value (<keras.optimizers.schedules.learning_rate_schedule.ExponentialDecay object at 0x00000247A305AD60>) with an unsupported type (<class 'keras.optimizers.schedules.learning_rate_schedule.ExponentialDecay'>) to a Tensor.
I have tried numerous ways, such as cast
,variable
,convert_to_tensor
, none of these worked, same error message raised. But when I just let learning rate equals a fix value, like 0.001, it works without a problem. I am just not sure what is going on with this class type thingy.
How can I work through this one, chiefs? Thank you very much.
Upvotes: 0
Views: 177