Flux tensor practice y = xw How do you initialize the vector x?

I am participating in the first practice of machine learning.

It is a monthly temperature prediction system.

train_t

has temperatures and train_x

weights for each data.

However, I have a question where is the initialization train_x

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from pprint import pprint 

x = tf.placeholder(tf.float32,[None,5])
w = tf.Variable(tf.zeros([5,1]))

y = tf.matmul(x,w)
t = tf.placeholder(tf.float32,[None,1])

loss = tf.reduce_sum(tf.square(y-t))
train_step = tf.train.AdamOptimizer().minimize(loss)

sess = tf.Session()
sess.run(tf.initialize_all_variables())

train_t = np.array([5.2,5.7,8.6,14.9,18.2,20.4,25.5,26.4,22.8,17.5,11.1,6.6]) #montly temperature
train_t = train_t.reshape([12,1])
train_x = np.zeros([12,5])

for row, month in enumerate(range(1,13)):
    for col, n in enumerate(range(0,5)):
        train_x[row][col] = month**n ## why initialize like this??


i = 0
for _ in range(10000):
    i += 1
    sess.run(train_step,feed_dict={x:train_x,t:train_t})
    if i % 1000 == 0:
        loss_val = sess.run(loss,feed_dict={x:train_x,t:train_t})
        print('step : %d,Loss: %f' % (i,loss_val))
        w_val = sess.run(w)
        pprint(w_val)

def predict(x):
    result = 0.0
    for n in range(0,5):
        result += w_val[n][0] * x**n
    return result

fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.set_xlim(1,12)
subplot.scatter(range(1,13),train_t)
linex = np.linspace(1,12,100)
liney = predict(linex)
subplot.plot(linex, liney)

      

However I don't understand here

for row, month in enumerate(range(1,13)): #
    for col, n in enumerate(range(0,5)): #
        train_x[row][col] = month**n  ## why initialize like this??

      

What does it mean? Does my book have comments on this? Why is train_x being initialized here?

+3


source to share


2 answers


In fact, this block of code:

train_t = np.array([5.2,5.7,8.6,14.9,18.2,20.4,25.5,26.4,22.8,17.5,11.1,6.6]) #montly temperature
train_t = train_t.reshape([12,1])
train_x = np.zeros([12,5])

for row, month in enumerate(range(1,13)):
    for col, n in enumerate(range(0,5)):
        train_x[row][col] = month**n

      



Generates your data. It initializes train_t

and train_x

, which is the data to be injected into placeholders

x

andt

train_t

is a tensor of temperatures train_x

is a tensor of a kind of weight for each temperature. They make up a dataset.

+1


source


Both train_x

and train_t

are arrays of your data training. In an array train_t

, you have the target of your model and it train_x

contains the input functions to your model.

The weight of your model (trained) is w

(which is the only one tf.Variable

in your code) that is randomly initialized.



The model you are training is a power of 4 (which is the maximum range(0, 5)

) polynomial in a linear variable month

that is in the range range(1, 13)

. This cut-off code generates functions for a polynomial of degree 4, starting with a linear variable month

.

0


source







All Articles