TensorFlowノート (2017/04/02)

各見出しは参考にしたページである。データは為替データに差し替えている。コードでエラーになる部分は修正しているが、警告の場合は原則そのまま。

Getting Started With TensorFlow

https://www.tensorflow.org/get_started/get_started

import forex_system as fs
import numpy as np
import tensorflow as tf
from datetime import datetime

fs.remove_temp_folder()

W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
loss = tf.reduce_sum(tf.square(linear_model - y))
optimizer = tf.train.GradientDescentOptimizer(0.000001) # 学習率が高いとnan発生。
train = optimizer.minimize(loss)
start = datetime.strptime('2016.01.01 00:00', '%Y.%m.%d %H:%M')
end = datetime.strptime('2016.12.31 23:59', '%Y.%m.%d %H:%M')
x_train = np.array(fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1)[start:end])
y_train = np.array(fs.i_roc('USDJPY', 5, 1, 0)[start:end])
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
    sess.run(train, {x:x_train, y:y_train})
curr_W, curr_b, curr_loss  = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))


W: [ -9.50196700e-05] b: [ -2.46166637e-05] loss: 162.686

MNIST For ML Beginners

https://www.tensorflow.org/get_started/mnist/beginners

import forex_system as fs
import numpy as np
import pandas as pd
import tensorflow as tf
from datetime import datetime

fs.remove_temp_folder()

x = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([10, 2]))
b = tf.Variable(tf.zeros([2]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 2])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),
                                              reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
start = datetime.strptime('2016.01.01 00:00', '%Y.%m.%d %H:%M')
end = datetime.strptime('2016.12.31 23:59', '%Y.%m.%d %H:%M')
x_train = fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1)[start:end]
for i in range(1, 10):
    x_train_temp = fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1 + i)[start:end]
    x_train = pd.concat([x_train, x_train_temp], axis=1)
x_train = np.array(x_train.values)
y_train_temp = fs.i_roc('USDJPY', 5, 1, 0)[start:end]
y_train_temp1 = y_train_temp.copy()
y_train_temp2 = y_train_temp.copy()
y_train_temp1[y_train_temp>=0.0] = 1
y_train_temp1[y_train_temp<0.0] = 0
y_train_temp2[y_train_temp>=0.0] = 0
y_train_temp2[y_train_temp<0.0] = 1
y_train = pd.concat([y_train_temp1, y_train_temp2], axis=1)
y_train = np.array(y_train.values)
size = len(x_train)
batch_size = 100
i = 0
while(True):
    if (i+1)*batch_size > size:
        break
    batch_xs, batch_ys = (x_train[i*batch_size:(i+1)*batch_size],
                          y_train[i*batch_size:(i+1)*batch_size])
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    i += 1
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: x_train, y_: y_train}))


0.487841

Deep MNIST for Experts

https://www.tensorflow.org/get_started/mnist/pros

import forex_system as fs
import numpy as np
import pandas as pd
import tensorflow as tf
from datetime import datetime

fs.remove_temp_folder()

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1], padding='SAME')

sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 10])
y_ = tf.placeholder(tf.float32, shape=[None, 2])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,10,1,1]) # or tf.reshape(x, [-1,1,10,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([3 * 1 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 3*1*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 2])
b_fc2 = bias_variable([2])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
start = datetime.strptime('2016.01.01 00:00', '%Y.%m.%d %H:%M')
end = datetime.strptime('2016.12.31 23:59', '%Y.%m.%d %H:%M')
x_train = fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1)[start:end]
for i in range(1, 10):
    x_train_temp = fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1 + i)[start:end]
    x_train = pd.concat([x_train, x_train_temp], axis=1)
x_train = np.array(x_train.values)
y_train_temp = fs.i_roc('USDJPY', 5, 1, 0)[start:end]
y_train_temp1 = y_train_temp.copy()
y_train_temp2 = y_train_temp.copy()
y_train_temp1[y_train_temp>=0.0] = 1
y_train_temp1[y_train_temp<0.0] = 0
y_train_temp2[y_train_temp>=0.0] = 0
y_train_temp2[y_train_temp<0.0] = 1
y_train = pd.concat([y_train_temp1, y_train_temp2], axis=1)
y_train = np.array(y_train.values)
size = len(x_train)
batch_size = 50
i = 0
while(True):
    if (i+1)*batch_size > size:
        break
    batch_xs, batch_ys = (x_train[i*batch_size:(i+1)*batch_size],
                          y_train[i*batch_size:(i+1)*batch_size])
    if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict={
            x:batch_xs, y_: batch_ys, keep_prob: 1.0})
        print("step %d, training accuracy %g"%(i, train_accuracy))
    train_step.run(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5})
    i += 1
print("test accuracy %g"%accuracy.eval(feed_dict={
    x: x_train, y_: y_train, keep_prob: 1.0}))


step 0, training accuracy 0
step 100, training accuracy 0.56
step 200, training accuracy 0.52
step 300, training accuracy 0.54
step 400, training accuracy 0.44
step 500, training accuracy 0.54
step 600, training accuracy 0.62
step 700, training accuracy 0.44
step 800, training accuracy 0.44
step 900, training accuracy 0.6
step 1000, training accuracy 0.48
step 1100, training accuracy 0.54
step 1200, training accuracy 0.54
step 1300, training accuracy 0.62
step 1400, training accuracy 0.4
step 1500, training accuracy 0.6
test accuracy 0.523694

tf.contrib.learn Quickstart

https://www.tensorflow.org/get_started/tflearn

以下のコードを「~py/test.py」ファイルとして保存する。

import forex_system as fs
import numpy as np
import pandas as pd
import tensorflow as tf
from datetime import datetime

def main():
    def get_train_inputs():
        x = tf.constant(training_set[:, 0:10])
        y = tf.constant(training_set[:, 10])
        return x, y

    def get_test_inputs():
        x = tf.constant(test_set[:, 0:10])
        y = tf.constant(test_set[:, 10])
        return x, y

    def new_samples():
        return np.array(
          [[6.4, 3.2, 4.5, 1.5],
           [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)
        predictions = list(classifier.predict(input_fn=new_samples))
        print(
            "New Samples, Class Predictions:    {}\n"
            .format(predictions))

    start = datetime.strptime('2016.01.01 00:00', '%Y.%m.%d %H:%M')
    end = datetime.strptime('2016.12.31 23:59', '%Y.%m.%d %H:%M')
    x = fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1)[start:end]
    for i in range(1, 10):
        x_temp = fs.i_zscore('USDJPY', 5, 12, 'MODE_SMA', 1 + i)[start:end]
        x = pd.concat([x, x_temp], axis=1)
    y = fs.i_roc('USDJPY', 5, 1, 0)[start:end]
    y_temp = y.copy()
    y[y_temp>=0.0] = 1
    y[y_temp<0.0] = 0
    xy = pd.concat([x, y], axis=1)
    xy = np.array(xy.values)
    size = len(xy)
    size_training = int(size * 0.8)
    training_set = xy[:size_training, :]
    test_set = xy[size_training:, :]
    feature_columns = [tf.contrib.layers.real_valued_column("", dimension=10)]
    classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                                hidden_units=[10, 20, 10],
                                                n_classes=2,
                                                model_dir="/tmp/test_model")
    classifier.fit(input_fn=get_train_inputs, steps=2000)
    accuracy_score = classifier.evaluate(input_fn=get_test_inputs,
                                         steps=1)["accuracy"]
    print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

if __name__ == "__main__":
    main()

以下のコマンドを実行する。

%run ~/py/test.py


(略)
Test Accuracy: 0.530198

コメント

非公開コメント