Я использую Tensorflow, чтобы соответствовать версии финансовой модели. Я подкладывал класс keras.model, чтобы написать какой -то пользовательский код. Кажется, все работает хорошо, когда я запускаю модель в стремлении к исполнению. Чтобы ускорить вещи, я хотел использовать функциональность, предоставленную декоратором TF.Function. Я украсил две функции: сам модель и пользовательский Train_step. Если я только украшу Train_step, все работает нормально. Как только я добавляю вызов модели, я получаю следующее сообщение об ошибке: < /p>
повысить valueError («Нет градиентов для любой переменной».) < /P>
Кто -нибудь видел что -то подобное или имеет идею, что здесь идет не так? < /p>
Большое спасибо за помощь. < /p>
Модель Файл: < /p>
import tensorflow as tf
import keras
class bondPriceModel(keras.Model):
def __init__(self, econDict, setupDict, GHDict, **kwargs):
super().__init__(**kwargs)
self.econDict = econDict
self.setupDict = setupDict
self.GHDict = GHDict
# Layers
self.hidden1 = keras.layers.Dense(setupDict['layerNodes'][0], activation='relu', kernel_initializer='he_normal', name='hidden1')
self.hidden2 = keras.layers.Dense(setupDict['layerNodes'][1], activation='relu', kernel_initializer='he_normal', name='hidden2')
self.outputs = keras.layers.Dense(1, activation='sigmoid', kernel_initializer='he_normal', bias_initializer='ones',
name='outputBP')
# Set none values for variables
@tf.function
def call(self, inputs):
x = self.hidden1(inputs)
x = self.hidden2(x)
x = self.outputs(x)
return x
def compile(self, optimizer, loss_fn):
super().compile(optimizer=optimizer, loss=loss_fn)
@tf.function
def train_step(self, x_batch_train, y_batch_train):
with tf.GradientTape() as tape:
y_pred = self(x_batch_train, training=True)
loss_value = self.loss(y_batch_train, y_pred)
grads = tape.gradient(loss_value, self.trainable_weights)
self.optimizer.apply_gradients(zip(grads, self.trainable_weights))
return loss_value
< /code>
Скрипт: < /p>
'''
This file contains the runtime code with all steps necessary to solve the underlying model.
So far implemented:
- Rep-Agent utility/consumption ratio solver using a fixed-point iteration
- Next: Implement risk-free bond pricing function.
'''
import random
import os
os.environ["OMP_NUM_THREADS"] = "4"
import numpy as np
import tensorflow as tf
from main.parameters import econDict
import keras
from helpers.bondPriceModel import bondPriceModel
# Specify Neural Network parameters
print('##### Setup Model #####')
setupDict = {}
setupDict['learningRate'] = 1e-5
setupDict['epochs'] = 150
setupDict['batchSize'] = 128
setupDict['nrOfBatches'] = 80
setupDict['inputShape'] = 6
setupDict['outputShape'] = 4
setupDict['layerNodes'] = [32 * 20, 32 * 20]
setupDict['seed'] = 1
setupDict['simLength'] = setupDict['nrOfBatches'] * setupDict['batchSize']
# Set up model for Bond Price
bondPriceNN = bondPriceModel(econDict, setupDict, [])
optimizerBP = keras.optimizers.Adam(learning_rate=setupDict['learningRate'])
loss_fnBP = keras.losses.MeanSquaredError(reduction="sum_over_batch_size", name="mean_squared_error")
bondPriceNN.compile(optimizerBP, loss_fnBP)
# Initialize the model states
random.seed(setupDict['seed'])
tf.random.set_seed(100)
# Fit first pass of the model
stateInit = np.vstack((np.ones(setupDict['simLength'])*0.15 + np.random.normal(0, 0.15/4, size=setupDict['simLength']),
np.ones(setupDict['simLength'])*0.005 + np.random.normal(0, 0.005/4, size=setupDict['simLength']),
np.ones(setupDict['simLength'])*0.2 + np.random.normal(0, 0.2/4, size=setupDict['simLength']),
np.random.normal(0, 0.0055, size=setupDict['simLength']),
np.random.normal(0, 0.012, size=setupDict['simLength']),
np.random.normal(0, 0.00104, size=setupDict['simLength']))).T
bondPriceInit = np.zeros(setupDict['simLength'])
for i in range(setupDict['simLength']):
bondPriceInit = 0.97 + np.random.normal(0, 0.005)
train_datasetBP = tf.data.Dataset.from_tensor_slices((tf.convert_to_tensor(stateInit, dtype=tf.float32), bondPriceInit))
train_datasetBP = train_datasetBP.shuffle(buffer_size=256).batch(setupDict['batchSize'])
for epoch in range(setupDict['epochs']):
for step, (x_batch_train, y_batch_train) in enumerate(train_datasetBP):
loss_valueBP = bondPriceNN.train_step(x_batch_train, y_batch_train)
# Log every 2 batches.
if step % 2 == 0:
print(
f"Training loss (for one batch) at step {step}: {float(loss_valueBP)}"
)
Подробнее здесь: https://stackoverflow.com/questions/794 ... call-error