Theano-简单培训示例 (Theano - Trivial Training Example)
Theano is quite useful in training neural networks where we have to repeatedly calculate cost, and gradients to achieve an optimum. On large datasets, this becomes computationally intensive. Theano does this efficiently due to its internal optimizations of the computational graph that we have seen earlier.
Theano在训练神经网络时非常有用,在神经网络中,我们必须反复计算成本和渐变以达到最佳效果。 在大型数据集上,这变得计算量很大。 Theano有效地做到了这一点,这是由于我们在内部已经对计算图进行了内部优化。
问题陈述 (Problem Statement)
We shall now learn how to use Theano library to train a network. We will take a simple case where we start with a four feature dataset. We compute the sum of these features after applying a certain weight (importance) to each feature.
现在我们将学习如何使用Theano库来训练网络。 我们以一个简单的案例为例,我们从一个四要素数据集开始。 在对每个特征应用一定的权重(重要性)之后,我们计算这些特征的总和。
The goal of the training is to modify the weights assigned to each feature so that the sum reaches a target value of 100.
训练的目的是修改分配给每个特征的权重,以使总和达到目标值100。
sum = f1 * w1 + f2 * w2 + f3 * w3 + f4 * w4
Where f1, f2, ... are the feature values and w1, w2, ... are the weights.
其中f1 , f2 ,...是特征值, w1 , w2 ,...是权重。
Let me quantize the example for a better understanding of the problem statement. We will assume an initial value of 1.0 for each feature and we will take w1 equals 0.1, w2 equals 0.25, w3 equals 0.15, and w4 equals 0.3. There is no definite logic in assigning the weight values, it is just our intuition. Thus, the initial sum is as follows −
让我量化示例以更好地理解问题陈述。 我们将为每个功能假设一个初始值1.0,我们将w1等于0.1 , w2等于0.25 , w3等于0.15 , w4等于0.3 。 分配权重值没有明确的逻辑,这只是我们的直觉。 因此,初始总和如下-
sum = 1.0 * 0.1 + 1.0 * 0.25 + 1.0 * 0.15 + 1.0 * 0.3
Which sums to 0.8. Now, we will keep modifying the weight assignment so that this sum approaches 100. The current resultant value of 0.8 is far away from our desired target value of 100. In Machine Learning terms, we define cost as the difference between the target value minus the current output value, typically squared to blow up the error. We reduce this cost in each iteration by calculating the gradients and updating our weights vector.
总计为0.8 。 现在,我们将继续修改权重分配,以使该总和接近100。当前的结果值0.8与我们期望的目标值100相去甚远。在机器学习术语中,我们将成本定义为目标值减去目标值之间的差当前输出值,通常平方以消除误差。 我们通过计算梯度并更新权重向量来降低每次迭代的成本。
Let us see how this entire logic is implemented in Theano.
让我们看看如何在Theano中实现整个逻辑。
声明变量 (Declaring Variables)
We first declare our input vector x as follows −
我们首先声明我们的输入向量x,如下所示:
x = tensor.fvector('x')
Where x is a single dimensional array of float values.
其中x是浮点值的一维数组。
We define a scalar target variable as given below −
我们定义一个标量目标变量,如下所示:
target = tensor.fscalar('target')
Next, we create a weights tensor W with the initial values as discussed above −
接下来,我们使用上述初始值创建权重张量W-
W = theano.shared(numpy.asarray([0.1, 0.25, 0.15, 0.3]), 'W')
定义Theano表达 (Defining Theano Expression)
We now calculate the output using the following expression −
我们现在使用以下表达式计算输出-
y = (x * W).sum()
Note that in the above statement x and W are the vectors and not simple scalar variables. We now calculate the error (cost) with the following expression −
注意,在上面的语句中, x和W是向量,而不是简单的标量变量。 我们现在使用以下表达式计算误差(成本)-
cost = tensor.sqr(target - y)
The cost is the difference between the target value and the current output, squared.
成本是目标值和当前输出之间的差,平方。
To calculate the gradient which tells us how far we are from the target, we use the built-in grad method as follows −
为了计算告诉我们离目标有多远的梯度,我们使用内置的grad方法,如下所示:
gradients = tensor.grad(cost, [W])
We now update the weights vector by taking a learning rate of 0.1 as follows −
我们现在通过采用如下的0.1的学习率来更新权重向量-
W_updated = W - (0.1 * gradients[0])
Next, we need to update our weights vector using the above values. We do this in the following statement −
接下来,我们需要使用上述值更新权重向量。 我们在以下语句中这样做-
updates = [(W, W_updated)]
定义/调用Theano函数 (Defining/Invoking Theano Function)
Lastly, we define a function in Theano to compute the sum.
最后,我们在Theano中定义一个函数来计算总和。
f = function([x, target], y, updates=updates)
To invoke the above function a certain number of times, we create a for loop as follows −
要多次调用上述函数,我们创建如下的for循环-
for i in range(10):
output = f([1.0, 1.0, 1.0, 1.0], 100.0)
As said earlier, the input to the function is a vector containing the initial values for the four features - we assign the value of 1.0 to each feature without any specific reason. You may assign different values of your choice and check if the function ultimately converges. We will print the values of the weight vector and the corresponding output in each iteration. It is shown in the below code −
如前所述,该函数的输入是一个包含四个要素初始值的向量-我们将1.0的值分配给每个要素而没有任何特定原因。 您可以分配不同的值,然后检查功能是否最终收敛。 我们将在每次迭代中打印权重向量的值和相应的输出。 它显示在下面的代码中-
print ("iteration: ", i)
print ("Modified Weights: ", W.get_value())
print ("Output: ", output)
完整程序清单 (Full Program Listing)
The complete program listing is reproduced here for your quick reference −
此处复制了完整的程序清单,供您快速参考-
from theano import *
import numpy
x = tensor.fvector('x')
target = tensor.fscalar('target')
W = theano.shared(numpy.asarray([0.1, 0.25, 0.15, 0.3]), 'W')
print ("Weights: ", W.get_value())
y = (x * W).sum()
cost = tensor.sqr(target - y)
gradients = tensor.grad(cost, [W])
W_updated = W - (0.1 * gradients[0])
updates = [(W, W_updated)]
f = function([x, target], y, updates=updates)
for i in range(10):
output = f([1.0, 1.0, 1.0, 1.0], 100.0)
print ("iteration: ", i)
print ("Modified Weights: ", W.get_value())
print ("Output: ", output)
When you run the program you will see the following output −
运行程序时,您将看到以下输出-
Weights: [0.1 0.25 0.15 0.3 ]
iteration: 0
Modified Weights: [19.94 20.09 19.99 20.14]
Output: 0.8
iteration: 1
Modified Weights: [23.908 24.058 23.958 24.108]
Output: 80.16000000000001
iteration: 2
Modified Weights: [24.7016 24.8516 24.7516 24.9016]
Output: 96.03200000000001
iteration: 3
Modified Weights: [24.86032 25.01032 24.91032 25.06032]
Output: 99.2064
iteration: 4
Modified Weights: [24.892064 25.042064 24.942064 25.092064]
Output: 99.84128
iteration: 5
Modified Weights: [24.8984128 25.0484128 24.9484128 25.0984128]
Output: 99.968256
iteration: 6
Modified Weights: [24.89968256 25.04968256 24.94968256 25.09968256]
Output: 99.9936512
iteration: 7
Modified Weights: [24.89993651 25.04993651 24.94993651 25.09993651]
Output: 99.99873024
iteration: 8
Modified Weights: [24.8999873 25.0499873 24.9499873 25.0999873]
Output: 99.99974604799999
iteration: 9
Modified Weights: [24.89999746 25.04999746 24.94999746 25.09999746]
Output: 99.99994920960002
Observe that after four iterations, the output is 99.96 and after five iterations, it is 99.99, which is close to our desired target of 100.0.
观察到经过四次迭代,输出为99.96 ,经过五次迭代,输出为99.99 ,这接近我们期望的目标100.0 。
Depending on the desired accuracy, you may safely conclude that the network is trained in 4 to 5 iterations. After the training completes, look up the weights vector, which after 5 iterations takes the following values −
根据所需的精度,您可以安全地得出结论,网络经过4到5次迭代训练。 训练完成后,查找权重向量,该向量在5次迭代后取以下值-
iteration: 5
Modified Weights: [24.8984128 25.0484128 24.9484128 25.0984128]
You may now use these values in your network for deploying the model.
现在,您可以在网络中使用这些值来部署模型。
翻译自: https://www.tutorialspoint.com/theano/theano_trivial_training_example.htm