我正在尝试在python中建立简单的数学时滞依赖模型:
标志= function(b0,.. bN,c0,.. cN,p0,..,pN),
其中:
滞后
当您观察到每行的标志值时,取决于先前的 N 个输入,因此我不知道提前的依赖深度,因此希望LSTM能够破解依赖关系。
> 源数据采用2D普通表的形式,其中:
<pre>asc_sort b c d e f g h m k l m n p flag</pre>
<pre>1 96 60 44 21 70 93 52 27 44 25 63 20 10 1</pre>
<pre>2 32 38 78 52 51 59 45 95 21 98 14 50 67 0</pre>
<pre>3 56 62 19 71 30 49 83 94 52 45 39 73 70 0</pre>
<pre>4 55 56 64 38 66 50 5 62 66 25 66 88 12 0</pre>
<pre>5 39 50 83 35 45 81 93 6 9 69 27 81 37 1</pre>
<pre>6 35 94 60 20 9 39 19 51 25 69 78 2 64 1</pre>
<pre>7 44 47 70 72 53 32 71 18 63 14 44 66 45 1</pre>
<pre>8 18 78 8 2 91 57 71 87 6 33 92 34 36 1</pre>
<pre>9 93 39 16 95 10 33 53 34 93 91 89 38 42 0</pre>
<pre>10 12 63 14 45 59 39 3 24 93 1 28 49 78 1</pre>
.............................................................................
<pre>9999 30 95 55 36 37 59 13 86 76 49 57 56 58 0</pre>
<pre>10000 16 99 3 71 73 11 26 20 50 10 21 53 65 0</pre>
我尝试将纯数据输入到tf.contrib.rnn.BasicLSTMCell并观察错误:
在static_rnn https://www.tensorflow.org/api_docs/python/tf/nn/static_rnn的文档中写道
输入:长度为T的输入列表,每个输入为张量为[batch_size,input_size]或此类元素的嵌套元组
import numpy as np
import tensorflow as tf
from sklearn import metrics
import random
from random import randint
n_lags = 190
num_rows = 10000 # number of rows in time-series data
n_input = 14 # count the number of predictors
def gen_rnd_dtfrm2(siz, ratio):
from random import random
import pandas as pd
import numpy as np
import warnings
import math
sz = int(siz)
xpreds = ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','p'];
x = pd.DataFrame(np.random.randint(1, 1000, [sz, xpreds.__len__()]) / 1000)
x['flg'] = 0.000000
x['ind'] = 0.000000
x['map'] = 0.00000000 # 0 - не получится сделать conditional assignment
for i in range(188, sz):
zabs = x.p[i - 87] / 10 * abs( 1 + x.b[i] / 100 - x.d[i - 45] / 500) * 1.78 * abs(x.e[i - 21] / 50000 + 1) ** 3.52 + (abs( math.cos(x.b[i - 130] / 100 - x.m[i] / 5 + 1)) ** 0.78 + x.d[i - 45] / 500) ** (3) * math.sin(
(x.e[i] - x.m[i] / 5 - x.c[i]) + x.k[i] + 5 * (x.e[i] / 1000000 * (x.j[i] / 100)) + 1)* (x.c[ i - 43] - x.m[i - 76] / 5 + 1) ** 4.78 * x.f[i - 185] / 5000 - x.p[i] / 10 - x.d[i - 76];
if zabs > -0.5:
x['flg'][i] = 1
else:
x['flg'][i] = 0
x['ind'] = x.index # just an index in order not to forget the sorting
rwind = np.transpose(range(0, sz))
x.map[rwind < ratio[0] * sz] = 0 # train
x.map[rwind >= ratio[1] * sz] = 2 # test
x.map[(rwind >= ratio[0] * sz) & (rwind < ratio[1] * sz)] = 1 # validation
return [x, xpreds]
ratio=np.array([0.7,0.85])
xx, xpreds =gen_rnd_dtfrm2(1e4, ratio)
flag = 'flg';
X_train = xx[xpreds][xx.map == 0]
X_train.shape
y_train = xx[flag][xx.map == 0].values.reshape(sum(xx.map == 0),1)
y_train.shape
n_hidden = 190 # Hidden layer num of cells in a layer
n_classes = 1 #
# updated for learning-rate decay
# calculated as: decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)
decaying_learning_rate = True
learning_rate = 0.0025 # used if decaying_learning_rate set to False
init_learning_rate = 0.005
decay_rate = 0.96 # the base of the exponential in the decay
decay_steps = 100000 # used in decay every 60000 steps with a base of 0.96
global_step = tf.Variable(0, trainable=False)
lambda_loss_amount = 0.0015
training_iters = X_train.shape[0] * 100 # Loop 100 times on the dataset, ie 100 epochs
batch_size = 500
display_iter = batch_size * 8 # To show test set accuracy during training
################## Build the network:
# Graph input/output
x = tf.placeholder(tf.float32, [None, n_input, n_lags])
y = tf.placeholder(tf.float32, [None, n_classes])
# Graph weights
weights = {
'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights
'out': tf.Variable(tf.random_normal([n_hidden, n_classes], mean=1.0))
}
biases = {
'hidden': tf.Variable(tf.random_normal([n_hidden])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
X_train.shape
# _X = tf.nn.relu( tf.matmul(X_train, weights['hidden']) + biases['hidden'] )
lstm_cell_1 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
lstm_cell_2 = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
lstm_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell_1, lstm_cell_2], state_is_tuple=True)
# here is the error generated
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell_1, X_train, dtype=tf.float32)
pred= tf.matmul(lstm_last_output, _weights['out']) + _biases['out']
# Loss, optimizer and evaluation
l2 = lambda_loss_amount * sum(tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables()) # L2 loss prevents this overkill neural network to overfit the data
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred)) + l2 # Softmax loss
# cost=tf.reduce_mean( -tf.reduce_sum( y* tf.log(prediction), axis=1))
if decaying_learning_rate:
learning_rate = tf.train.exponential_decay(init_learning_rate, global_step * batch_size, decay_steps, decay_rate,
staircase=True)
# decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps) #exponentially decayed learning rate
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost, global_step=global_step) # Adam Optimizer
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
################## Train the network:
test_losses = []
test_accuracies = []
train_losses = []
train_accuracies = []
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# Perform Training steps with "batch_size" amount of data at each loop.
# Elements of each batch are chosen randomly, without replacement, from X_train,
# restarting when remaining datapoints < batch_size
step = 1
time_start = time.time()
unsampled_indices = range(0, len(X_train))
g = tf.get_default_graph()
graph_op = g.get_operations()
with open('s:/escape/get_default_graph_opearations.txt', 'w') as f:
for i in graph_op:
f.write(str(i))
print(tf.__version__)
while step * batch_size <= training_iters:
if len(unsampled_indices) < batch_size:
unsampled_indices = range(0, len(X_train))
X_train.shape
y_train.shape
batch_xs, raw_labels, unsampled_indicies = extract_batch_size(X_train, y_train, list(unsampled_indices), batch_size)
batch_xs.shape
batch_ys = raw_labels
raw_labels.shape
_, loss, acc = sess.run( [optimizer, cost, accuracy], feed_dict={ x: batch_xs, y: batch_ys } )
train_losses.append(loss)
train_accuracies.append(acc)
# Evaluate network only at some steps for faster training:
if (step * batch_size % display_iter == 0) or (step == 1) or (step * batch_size > training_iters):
# To not spam console, show training accuracy/loss in this "if"
print("Iter #" + str(step * batch_size) + \
": Learning rate = " + "{:.6f}".format(sess.run(learning_rate)) + \
": Batch Loss = " + "{:.6f}".format(loss) + \
", Accuracy = {}".format(acc))
loss, acc = sess.run( [cost, accuracy], feed_dict={ x: X_test, y: y_test } )
test_losses.append(loss)
test_accuracies.append(acc)
print("PERFORMANCE ON TEST SET: " + \
"Batch Loss = {}".format(loss) + \
", Accuracy = {}".format(acc))
step += 1
print("Optimization Finished!")
错误是由字符串生成的:
输出,状态= tf.contrib.rnn.static_rnn(lstm_cell_1,X_train,dtype = tf.float32)
File "C:\Users\roma\PycharmProjects\1st_py\venv\lib\site-packages\tensorflow\python\ops\rnn.py", line 1290, in static_rnn raise TypeError("inputs must be a sequence") TypeError: inputs must be a sequence
我想知道: