2016-12-03 75 views
0

我正在学习TensorFlow。如何在不给出公式的情况下使用TensorFlow训练模型?

我在Introduction关于代码的一个问题:

import tensorflow as tf 
import numpy as np 

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3 
x_data = np.random.rand(100).astype(np.float32) 
y_data = x_data * 0.1 + 0.3 

# Try to find values for W and b that compute y_data = W * x_data + b 
# (We know that W should be 0.1 and b 0.3, but TensorFlow will 
# figure that out for us.) 
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) 
b = tf.Variable(tf.zeros([1])) 
y = W * x_data + b 

# Minimize the mean squared errors. 
loss = tf.reduce_mean(tf.square(y - y_data)) 
optimizer = tf.train.GradientDescentOptimizer(0.5) 
train = optimizer.minimize(loss) 

# Before starting, initialize the variables. We will 'run' this first. 
init = tf.global_variables_initializer() 

# Launch the graph. 
sess = tf.Session() 
sess.run(init) 

# Fit the line. 
for step in range(201): 
    sess.run(train) 
    if step % 20 == 0: 
     print(step, sess.run(W), sess.run(b)) 

# Learns best fit is W: [0.1], b: [0.3] 

这个节目学习的W和B最合适的。

如果我不知道公式(y = W * x_data + b),我该如何训练模型?

例如,这是一个训练集:

{input = {{1,1}, {1,2}, {2,3}, ... }, target = {2, 3, 5, ...}} 

如何训练的功能(A,B)〜=(A + B)?

回答

0

在大多数情况下,我们不知道客观公式的确切形式。因此,我们必须设计一个函数,并尝试用这个函数来逼近客观公式。 在神经网络中,公式由网络体系结构(例如多层感知器或递归神经网络)和超参数(例如,隐藏层的数量,隐层中神经元的数量)定义。例如,在这个特定情况下,可以假设近似函数具有(y = Wa + Ub + C - 一个线性感知器)的形式,并将此函数的参数(W,U,C)使用给出的数据近似客观公式(y = a + b)的参数。

0

神经网络是一个通用函数逼近器:也就是说,对于任何函数(线性,多项式等),神经网络可以通过隐藏层中的足够节点和激活函数来逼近它。非线性激活函数(例如sigmoid,tanh,ReLU)将“弯曲”由Wx + b产生的线性边界为非线性的。

相关问题