FailedPreconditionError:从容器:本地主机读取资源变量rnn / multi_rnn_cell / cell_1 / lstm_cell / kernel时出错。这可能意味着该变量未初始化。找不到:容器localhost不存在。
defaultPosition={{ x: '50%', y: '50%' }}
完全错误:
tf.compat.v1.reset_default_graph()
graph = tf.compat.v1.get_default_graph()
with graph.as_default():
batch_size = 1000
features = 1
alpha = 0.50
time_steps = paths_train.shape[0]
nodes = [62,46,46,1]
name='rnn_050'
S_t_input = tf.compat.v1.placeholder(tf.float32, [time_steps, batch_size, features])
K = tf.compat.v1.placeholder(tf.float32, batch_size)
alpha = tf.compat.v1.placeholder(tf.float32)
S_T = S_t_input[-1,:,0]
dS = S_t_input[1:, :, 0] - S_t_input[0:-1, :, 0]
S_t = tf.unstack(S_t_input[:-1, :,:], axis=0)
lstm = tf.compat.v1.nn.rnn_cell.MultiRNNCell([tf.compat.v1.nn.rnn_cell.LSTMCell(n) for n in nodes])
strategy, state = tf.compat.v1.nn.static_rnn(lstm, S_t, initial_state=lstm.zero_state(batch_size, tf.float32), dtype=tf.float32)
strategy = tf.reshape(strategy, (time_steps-1, batch_size))
option = tf.maximum(S_T-K, 0)
Hedging_PnL = - option + tf.reduce_sum(dS*strategy, axis=0)
Hedging_PnL_Paths = - option + dS*strategy
# Calculate the CVaR for a given confidence level alpha
# Take the 1-alpha largest losses (top 1-alpha negative PnLs) and calculate the mean
CVaR, idx = tf.nn.top_k(-Hedging_PnL, tf.cast((1-alpha)*batch_size, tf.int32))
CVaR = tf.reduce_mean(CVaR)
train = tf.compat.v1.train.AdamOptimizer().minimize(CVaR)
saver = tf.compat.v1.train.Saver()
modelname = name
sample_size = paths_train.shape[1]
batch_size= batch_size
idx = np.arange(sample_size)
start = dt.datetime.now()
epochs = 101
strikes = np.ones(paths_train.shape[1])*100
riskaversion = 0.99 # alpha
for epoch in range(epochs):
# Save the hedging Pnl for each batch
pnls = []
strategies = []
np.random.shuffle(idx)
for i in range(int(sample_size/batch_size)):
indices = idx[i*batch_size : (i+1)*batch_size]
batch = paths_train[:,indices,:]
with tf.compat.v1.Session() as sess:
_, pnl, strategy = sess.run([train, Hedging_PnL, strategy], {S_t_input: batch,K : strikes[indices],alpha: riskaversion})
pnls.append(pnl)
strategies.append(strategy)
CVaR = np.mean(-np.sort(np.concatenate(pnls))[:int((1-riskaversion)*sample_size)])
有人知道如何解决这个问题吗?