我试图用Tensorflow建立一个神经网络,但我很难弄清楚如何制作一个简单的numpy数组/列表输入的网络。我尝试过遵循Tensorflow教程,但大多数使用mnist手写数据集。
例如,我想要像这样的简单X和Y数据。
X = np.array(([3, 5], [5, 1], [10, 2]), dtype=float)
Y = np.array(([75], [82], [93]), dtype=float)其中X包括睡眠时间和为考试而花的学习时间。Y由在这些考试中得到的相应分数组成。所有网络都必须由两个输入节点、3-5个隐藏节点和一个输出节点组成。
我一直试图遵循的例子是perceptron.py
发布于 2017-09-22 17:01:15
以下内容来自堆栈溢出文档(存档在这里)中的“从Tensorflow开始”;工科、利平斯基、尼古拉斯、多利克、史蒂文和疯垫的版权2017年;CC由-SA3.0授权。在archive.org中可以找到完整的堆栈溢出文档内容的归档文件,其中本例通过主题ID: 856 (例如: 4069 )进行索引。
Tensorflow不仅仅是一个深层次的学习框架。它是一种以并行和分布式的方式执行一般数学运算的通用计算框架。下文将介绍这方面的一个例子。
线性回归
一个常用且计算相当简单的基本统计示例是将一条线拟合到数据集。在tensorflow中这样做的方法将在下面的代码和注释中描述。
(TensorFlow)脚本的主要步骤是:
x_ph,y_ph)和变量(W,b)init)y_pred、loss、train_op)上的操作sess)sess.run(init))sess.run([train_op, loss], feed_dict={x_ph: x, y_ph: y}))图形构造是使用Python完成的(也可以使用TensorFlow API完成)。运行图形将调用低级C++例程。
'''
function: create a linear model which try to fit the line
y = x + 2 using SGD optimizer to minimize
root-mean-square(RMS) loss function
'''
import tensorflow as tf
import numpy as np
# number of epoch
num_epoch = 100
# training data x and label y
x = np.array([0., 1., 2., 3.], dtype=np.float32)
y = np.array([2., 3., 4., 5.], dtype=np.float32)
# convert x and y to 4x1 matrix
x = np.reshape(x, [4, 1])
y = np.reshape(y, [4, 1])
# test set(using a little trick)
x_test = x + 0.5
y_test = y + 0.5
# This part of the script builds the TensorFlow graph using the Python API
# First declare placeholders for input x and label y
# Placeholders are TensorFlow variables requiring to be explicitly fed by some
# input data
x_ph = tf.placeholder(tf.float32, shape=[None, 1])
y_ph = tf.placeholder(tf.float32, shape=[None, 1])
# Variables (if not specified) will be learnt as the GradientDescentOptimizer
# is run
# Declare weight variable initialized using a truncated_normal law
W = tf.Variable(tf.truncated_normal([1, 1], stddev=0.1))
# Declare bias variable initialized to a constant 0.1
b = tf.Variable(tf.constant(0.1, shape=[1]))
# Initialize variables just declared
init = tf.initialize_all_variables()
# In this part of the script, we build operators storing operations
# on the previous variables and placeholders.
# model: y = w * x + b
y_pred = x_ph * W + b
# loss function
loss = tf.mul(tf.reduce_mean(tf.square(tf.sub(y_pred, y_ph))), 1. / 2)
# create training graph
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# This part of the script runs the TensorFlow graph (variables and operations
# operators) just built.
with tf.Session() as sess:
# initialize all the variables by running the initializer operator
sess.run(init)
for epoch in xrange(num_epoch):
# Run sequentially the train_op and loss operators with
# x_ph and y_ph placeholders fed by variables x and y
_, loss_val = sess.run([train_op, loss], feed_dict={x_ph: x, y_ph: y})
print('epoch %d: loss is %.4f' % (epoch, loss_val))
# see what model do in the test set
# by evaluating the y_pred operator using the x_test data
test_val = sess.run(y_pred, feed_dict={x_ph: x_test})
print('ground truth y is: %s' % y_test.flatten())
print('predict y is : %s' % test_val.flatten())https://stackoverflow.com/questions/40795976
复制相似问题