训练TensorFlow预测总和

时间:2016-09-07 16:39:27

标签: python tensorflow

TensorFlow提供的示例对于入门来说有点复杂,所以我试图教TensorFlow训练一个神经网络来预测三个二进制数字的总和。网络将其中两个作为输入;第三个是未知的。因此,“最佳”网络会猜测总和将是两个已知位的总和,加上未知位的1/2。假设“损失”函数是网络预测值与实际值之差的平方。

我已编写代码来生成试验:

autocmd CursorMoved,CursorMovedI <buffer> if IsSpecialLine() | if &textwidth != 0 | setlocal textwidth=0 | endif | else | if &textwidth != 80 | setlocal textwidth=80 | endif | endif

输出表明这很有效:

import tensorflow as tf
import numpy as np
from random import randint

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('batch_size', 5, 'Batch size.  ')
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('dim1', 3, 'layer size')
flags.DEFINE_integer('training_epochs', 10, 'Number of passes through the main training loop')


def ezString(list):
    #debugging code so I can see what is going on
    listLength = len(list)
    r = ''
    for i in range(listLength):
        value = list[i]
        valueString = str(value)
        r = r + ' '
        r = r + valueString
    return r

def generateTrial():
    inputs = np.zeros(2, dtype=np.int)
    for i in range(2):
        inputs[i] = randint(0,1)
    unknownInput = randint(0,1)
    sum = 0
    for j in range(2):
        sum = sum + inputs[j]
    sum = sum + unknownInput
    inputTensor = tf.pack(inputs)
    print 'inputs' + ezString(inputs)
    print 'unknown ' + str(unknownInput)
    print 'sum ' + str(sum)
    print ''
    return inputTensor, sum

def printTensor(tensor):
    sh = tensor.get_shape()
    print(sh)

def placeholder_inputs(size):
    output_placeholder = tf.placeholder(tf.int32, shape=(size))
    input_placeholder = tf.placeholder(tf.int32, shape=(size,
                                                                                                                 2))
    return input_placeholder, output_placeholder

def fill_feed_dict(inputs_pl, output_pl):
    print ('Filling feed dict')
    inputs_placeholder, output_placeholder = placeholder_inputs(FLAGS.batch_size)
    inputs = []
    outputs = []
    for i in range(FLAGS.batch_size):
        input, output = generateTrial()
        inputTensor = tf.pack(input)
        inputs.append(input)
        outputs.append(output)
    inputs_placeholder = tf.pack(inputs)
    outputs_placeholder = tf.pack(outputs)

def run_training():
    input_placeholder, output_placeholder = placeholder_inputs(FLAGS.batch_size)
    fill_feed_dict(input_placeholder, output_placeholder)
    printTensor(input_placeholder)
    printTensor(output_placeholder)




run_training()

但我不知道如何完成它。特别是,我需要定义一个损失函数,我还需要连接起来,以便我的网络输出用于生成进一步训练步骤的猜测。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我不确定这段代码是否是你想要的,但我希望你会发现它有用。平均误差实际上是沿着迭代减少的,虽然我还没有测试它进行预测,所以这取决于你!

import tensorflow as tf
import numpy as np
from random import randint

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('batch_size', 50, 'Batch size.')
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('dim1', 3, 'layer size')
flags.DEFINE_integer('training_epochs', 10, 'Number of passes through the main training loop')
flag.DEFINE_integer('num_iters', 100, 'Number of iterations')


def ezString(list):
    #debugging code so I can see what is going on
    listLength = len(list)
    r = ''
    for i in range(listLength):
        value = list[i]
        valueString = str(value)
        r = r + ' '
        r = r + valueString
    return r

def generateTrial():
    inputs = np.zeros(2, dtype = np.float)
    for i in range(2):
        inputs[i] = randint(0, 1)
    unknownInput = randint(0, 1)
    um = 0
    for j in range(2):
        sum = sum + inputs[j]
    sum = sum + unknownInput
    inputTensor = np.asarray(inputs)
    return inputTensor, sum

def printTensor(tensor):
    sh = tensor.get_shape()
    print(sh)

def placeholder_inputs(size):
    output_placeholder = tf.placeholder(tf.float32, shape=(size))
    input_placeholder = tf.placeholder(tf.float32, shape=(size, 2))
    return input_placeholder, output_placeholder

def fill_feed_dict(inputs_pl, output_pl):
    inputs = []
    outputs = []
    for i in range(FLAGS.batch_size):
        input, output = generateTrial()
        inputs.append(input)
        outputs.append(output)

    return {inputs_pl: inputs, output_pl: outputs}

def loss(y, pred):
    return tf.reduce_mean(tf.pow(y - pred, 2))

def NN(x, y, W1, b1, W2, b2):
    layer1 = tf.add(tf.matmul(x, W1), b1)
    layer1 = tf.nn.relu(layer1)
    output = tf.add(tf.matmul(layer1, W2), b2)
    return output, loss(y, output)

def get_params(dim_hidden):
    with tf.variable_scope('nn_params'):
        return tf.Variable(tf.truncated_normal([2, dim_hidden], stddev = 0.05)), tf.Variable(0.0, (dim_hidden)),\
        tf.Variable(tf.truncated_normal([dim_hidden, 1], stddev = 0.05)), tf.Variable(0.0, 1)

def run_training():
    input_placeholder, output_placeholder = placeholder_inputs(FLAGS.batch_size)
    W1, b1, W2, b2 = get_params(FLAGS.dim1)
    pred, loss = NN(input_placeholder, output_placeholder, W1, b1, W2, b2)
    optm = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss)
    init = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init)

    for iters in range(FLAGS.num_iters):
        l, _ = sess.run([loss, optm], feed_dict = fill_feed_dict(input_placeholder, output_placeholder))
        print l, iters + 1