Tutorial 1

Esse tutorial tem como objetivo explorar os conceitos básicos do Tensorflow. Detalhes de como instalar o TensorFlow podem ser encontrados em: https://www.tensorflow.org/.

Links de referência para esse material:

O código a seguir verifica se o tensorflow foi instalado e exibe a versão atual


In [1]:
import tensorflow as tf
tf.__version__


Out[1]:
'1.0.0'

Para entender um pouco do funcionamento do tensorflow, vamos criar a "rede" a seguir:


In [2]:
#Criando os nós:

node1 = tf.constant(3.0, tf.float32) #Cria o nó "a" com o valor 3 do tipo float
node2 = tf.constant(4.0) # Cria o nó "b" com o valor 4 (implicitamente ele também é do tipo float)
node3 = tf.add(node1, node2) # Cria o nó raiz aplicando as operações de soma nos nós a e b

In [3]:
print("node1:", node1, "node2:", node2)
print("node3:", node3)


node1: Tensor("Const:0", shape=(), dtype=float32) node2: Tensor("Const_1:0", shape=(), dtype=float32)
node3: Tensor("Add:0", shape=(), dtype=float32)

In [4]:
#Vamos criar uma seção do TensorFlow para executar as operações necessárias:

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


Out[4]:
7.0

O workflow de trabalho com o tensorflow pode ser dividio em 3 partes:

  • Construção do grafo utilizando as operações do Tensorflow In[3]
  • Alimentar com os dados e executar as operações do grafo (sess.run(op)) In[6]
  • Atualizar as variáveis do grafo (e retornar os valores de saída) In[6]

Na primeira solução, criamos o grafo já com os valores pré-definidos. No entanto, é interessante que esses valores sejam alimentados com os valore de entrada. Para isso, vamos criar os placeholder. Detalhes sobre feed e placeholder podem ser encontrados em: https://www.tensorflow.org/programmers_guide/reading_data#feeding


In [5]:
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

adder_node = a + b # esse comando tem a mesma função de tf.add(node1, node2) do exemplo anterior

print(sess.run(adder_node, feed_dict={a: 3, b: 4}))


7.0

In [6]:
print(sess.run(adder_node, feed_dict={a: [1, 3], b:[2, 4]}))


[ 3.  7.]

Um outro conceito básico importante é o conceito de Variáveis no Tensorflow. Quando criamos um modelo, as veriáveis são utilizadas para manter e atualizar os parâmetros deste modelo. Detalhes sobre variáveis podem ser encontrados em: https://www.tensorflow.org/programmers_guide/variables

O exemplo a seguir, mostra brevemente como seria o uso de váriaveis no código.


In [7]:
# Cria as variáveis 

weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name="weights")
biases = tf.Variable(tf.zeros([200]), name="biases")

# Adiciona um operação que inicializa as variáveis que serão utilizadas no modelo 
init_op = tf.global_variables_initializer()

# Quando for rodar o modelo, executa a inicialização das operações

with tf.Session() as sess:
    sess.run(init_op)
    # ... 
    # Use the model 
    # ...

Outro conceito importante é o conceito de Tensor. No tensorflow tudo é Tensor (Everything is Tensor). Você pode imaginar o Tensor como sendo um array n-dimensional ou uma lista. Um tensor possui um tipo estático e a dimensão é determinada de forma dinâmica. Uma observação importante é que apenas tensors podem ser passados entre os nós de um grafo no tensorflow. Dealhes podem ser encontrados em: https://www.tensorflow.org/programmers_guide/dims_types

Para ilustrar esses componentes, vamos trabalhar com o modelo. O próposito é criar o modelo de regressão linear utilizando o tensorflow. Para treinar valos utilizar os seguinte modelo:

  • Hypothesis

$H(x) = Wx + b$

  • Const function

$cost(W, b) = \frac{1}{m}\sum_{i=1}^{m}(H(x^{(i)})-y^{(i)})^2$

  • Gradient descent algorithm

In [8]:
# Cosntruindo o grafo utilizando operações do Tensorflow

# Dados de entrada

x_train = [1, 2, 3]
y_train = [1, 2, 3]

# Criando a operação da Hypothesis

W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

# XW + b

hypothesis = x_train * W + b

In [9]:
# cost function

cost = tf.reduce_mean(tf.square(hypothesis - y_train))

In [10]:
# Gradient Descent

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

Uma vez que o modelo foi criado, o próximo passo é o de executar e atualizar o grafo e pegar o resultado final.


In [11]:
# Criar a sessão do grafo

sess = tf.Session()

# Inicializa as variáveis no grafo
sess.run(tf.global_variables_initializer())

#Treinamento

for step in range(2001):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(cost), sess.run(W), sess.run(b))


0 3.53006 [ 0.5189535] [-0.87523979]
20 0.0718851 [ 1.16400707] [-0.56029165]
40 0.0368705 [ 1.21556818] [-0.50788742]
60 0.0332287 [ 1.21108079] [-0.48153594]
80 0.0301765 [ 1.20169818] [-0.45866919]
100 0.0274068 [ 1.1922704] [-0.43709102]
120 0.0248913 [ 1.18323922] [-0.41654721]
140 0.0226067 [ 1.17462814] [-0.39697087]
160 0.0205317 [ 1.16642129] [-0.3783147]
180 0.0186472 [ 1.15860009] [-0.36053526]
200 0.0169357 [ 1.15114653] [-0.34359142]
220 0.0153813 [ 1.14404309] [-0.3274439]
240 0.0139695 [ 1.13727355] [-0.31205514]
260 0.0126874 [ 1.13082218] [-0.29738969]
280 0.0115229 [ 1.1246742] [-0.28341347]
300 0.0104652 [ 1.11881483] [-0.27009413]
320 0.0095047 [ 1.11323106] [-0.25740069]
340 0.00863231 [ 1.10790956] [-0.24530376]
360 0.00784002 [ 1.10283816] [-0.23377538]
380 0.00712042 [ 1.09800518] [-0.22278877]
400 0.00646688 [ 1.09339929] [-0.21231855]
420 0.00587334 [ 1.08900988] [-0.20234039]
440 0.00533426 [ 1.08482671] [-0.19283113]
460 0.00484466 [ 1.08084023] [-0.18376881]
480 0.0044 [ 1.07704103] [-0.17513236]
500 0.00399614 [ 1.07342041] [-0.16690178]
520 0.00362936 [ 1.06997001] [-0.15905806]
540 0.00329625 [ 1.06668162] [-0.15158296]
560 0.00299371 [ 1.06354773] [-0.14445913]
580 0.00271893 [ 1.06056154] [-0.13767019]
600 0.00246938 [ 1.05771518] [-0.13120022]
620 0.00224274 [ 1.05500281] [-0.1250343]
640 0.00203689 [ 1.05241776] [-0.11915813]
660 0.00184994 [ 1.04995441] [-0.11355815]
680 0.00168014 [ 1.04760671] [-0.1082213]
700 0.00152593 [ 1.04536939] [-0.10313529]
720 0.00138588 [ 1.04323733] [-0.09828834]
740 0.00125868 [ 1.04120541] [-0.09366926]
760 0.00114315 [ 1.03926873] [-0.08926723]
780 0.00103823 [ 1.03742325] [-0.08507195]
800 0.000942936 [ 1.03566456] [-0.08107387]
820 0.00085639 [ 1.03398848] [-0.07726369]
840 0.000777787 [ 1.03239107] [-0.07363258]
860 0.000706397 [ 1.03086877] [-0.07017212]
880 0.000641559 [ 1.02941799] [-0.06687426]
900 0.000582672 [ 1.02803552] [-0.06373138]
920 0.000529193 [ 1.0267179] [-0.0607362]
940 0.000480623 [ 1.02546239] [-0.05788184]
960 0.000436506 [ 1.02426565] [-0.05516162]
980 0.000396447 [ 1.02312529] [-0.05256923]
1000 0.000360057 [ 1.02203846] [-0.05009869]
1020 0.000327011 [ 1.02100277] [-0.04774427]
1040 0.000296994 [ 1.0200156] [-0.04550047]
1060 0.000269736 [ 1.01907492] [-0.043362]
1080 0.000244978 [ 1.01817858] [-0.04132406]
1100 0.000222492 [ 1.01732421] [-0.03938197]
1120 0.00020207 [ 1.01651001] [-0.03753113]
1140 0.000183523 [ 1.01573408] [-0.0357673]
1160 0.000166678 [ 1.01499462] [-0.03408637]
1180 0.000151379 [ 1.01428986] [-0.03248442]
1200 0.000137485 [ 1.01361835] [-0.03095775]
1220 0.000124866 [ 1.01297832] [-0.02950281]
1240 0.000113406 [ 1.01236844] [-0.0281163]
1260 0.000102998 [ 1.01178718] [-0.02679496]
1280 9.35438e-05 [ 1.01123321] [-0.02553569]
1300 8.49576e-05 [ 1.01070535] [-0.02433561]
1320 7.71606e-05 [ 1.01020229] [-0.023192]
1340 7.00788e-05 [ 1.00972271] [-0.02210212]
1360 6.36458e-05 [ 1.00926578] [-0.02106336]
1380 5.78049e-05 [ 1.00883031] [-0.02007347]
1400 5.25003e-05 [ 1.00841534] [-0.01913009]
1420 4.76807e-05 [ 1.00801992] [-0.01823108]
1440 4.33049e-05 [ 1.00764298] [-0.01737431]
1460 3.9331e-05 [ 1.00728381] [-0.01655779]
1480 3.57201e-05 [ 1.00694156] [-0.01577965]
1500 3.24424e-05 [ 1.00661528] [-0.01503808]
1520 2.94645e-05 [ 1.00630438] [-0.01433135]
1540 2.67598e-05 [ 1.00600815] [-0.01365781]
1560 2.43034e-05 [ 1.00572574] [-0.01301597]
1580 2.20733e-05 [ 1.00545669] [-0.01240429]
1600 2.00471e-05 [ 1.00520027] [-0.01182135]
1620 1.82077e-05 [ 1.00495589] [-0.0112658]
1640 1.65355e-05 [ 1.00472295] [-0.01073635]
1660 1.50187e-05 [ 1.0045011] [-0.01023181]
1680 1.36398e-05 [ 1.00428951] [-0.00975101]
1700 1.23881e-05 [ 1.00408792] [-0.00929278]
1720 1.12517e-05 [ 1.00389588] [-0.0088561]
1740 1.02187e-05 [ 1.00371277] [-0.00843992]
1760 9.28121e-06 [ 1.00353825] [-0.00804333]
1780 8.42909e-06 [ 1.00337207] [-0.00766537]
1800 7.65564e-06 [ 1.00321364] [-0.00730518]
1820 6.95293e-06 [ 1.00306261] [-0.00696186]
1840 6.31493e-06 [ 1.00291872] [-0.00663473]
1860 5.73564e-06 [ 1.00278163] [-0.00632298]
1880 5.2091e-06 [ 1.00265074] [-0.00602588]
1900 4.73083e-06 [ 1.00252628] [-0.0057427]
1920 4.29678e-06 [ 1.00240755] [-0.00547285]
1940 3.90244e-06 [ 1.00229442] [-0.0052157]
1960 3.54437e-06 [ 1.00218666] [-0.00497062]
1980 3.2191e-06 [ 1.0020839] [-0.00473704]
2000 2.92374e-06 [ 1.00198603] [-0.00451447]

Vamos exibir os dados originais e o modelo treinado.


In [14]:
#Graphic display
import matplotlib.pyplot as plt
plt.plot(x_train, y_train, 'ro', label='Original data')
plt.plot(x_train, sess.run(W) * x_train + sess.run(b), label='Fitted line')
plt.legend()
plt.show()


Vamos mudar nossa implementação para que use os conceitos de PlaceHolder e que a gente possa alimentar nosso modelo com os dados. Nessa etapa vamos utilizar mais dados. As listas a seguir e o gráfico mostram a distribuição dos dados que a gente vai criar.

x_train = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167, 7.042,10.791,5.313,7.997,5.654,9.27,3.1]

y_train = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221, 2.827,3.465,1.65,2.904,2.42,2.94,1.3]

In [15]:
x_train = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1]
y_train = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3]

plt.plot(x_train, y_train, 'ro', label='Original data')
plt.show()



In [16]:
# Modelo completo utilizando o placeholder

W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

hypothesis = X * W + b

cost = tf.reduce_mean(tf.square(hypothesis - Y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

for step in range(2001):
    sess.run(train, feed_dict={X: x_train, Y: y_train})
    if step % 20 == 0:
        print(step, sess.run(cost, feed_dict={X: x_train, Y: y_train}), 
              sess.run(W), sess.run(b))


0 0.23247 [ 0.28270796] [ 0.33447239]
20 0.177582 [ 0.31338245] [ 0.36103925]
40 0.175384 [ 0.31045255] [ 0.38181081]
60 0.173389 [ 0.30766168] [ 0.40159675]
80 0.17158 [ 0.30500326] [ 0.42044395]
100 0.169938 [ 0.30247095] [ 0.43839678]
120 0.168448 [ 0.30005881] [ 0.45549783]
140 0.167096 [ 0.29776111] [ 0.47178733]
160 0.16587 [ 0.29557246] [ 0.48730397]
180 0.164757 [ 0.29348764] [ 0.50208437]
200 0.163747 [ 0.29150176] [ 0.51616341]
220 0.162831 [ 0.28961006] [ 0.52957463]
240 0.162 [ 0.28780818] [ 0.54234928]
260 0.161245 [ 0.28609177] [ 0.55451787]
280 0.160561 [ 0.28445679] [ 0.56610894]
300 0.15994 [ 0.28289944] [ 0.57715011]
320 0.159376 [ 0.28141594] [ 0.58766729]
340 0.158865 [ 0.28000286] [ 0.5976854]
360 0.158401 [ 0.27865681] [ 0.60722834]
380 0.15798 [ 0.27737463] [ 0.6163184]
400 0.157598 [ 0.2761533] [ 0.62497711]
420 0.157252 [ 0.2749899] [ 0.63322496]
440 0.156937 [ 0.27388173] [ 0.64108145]
460 0.156652 [ 0.27282614] [ 0.64856523]
480 0.156393 [ 0.2718206] [ 0.65569389]
500 0.156158 [ 0.27086282] [ 0.66248417]
520 0.155945 [ 0.26995048] [ 0.66895223]
540 0.155752 [ 0.26908141] [ 0.6751135]
560 0.155576 [ 0.26825359] [ 0.68098247]
580 0.155417 [ 0.26746505] [ 0.68657285]
600 0.155272 [ 0.26671392] [ 0.69189811]
620 0.155141 [ 0.26599842] [ 0.69697064]
640 0.155022 [ 0.26531687] [ 0.70180237]
660 0.154915 [ 0.26466769] [ 0.70640492]
680 0.154817 [ 0.26404929] [ 0.71078908]
700 0.154728 [ 0.26346022] [ 0.71496528]
720 0.154647 [ 0.26289913] [ 0.71894324]
740 0.154574 [ 0.26236463] [ 0.72273248]
760 0.154508 [ 0.26185551] [ 0.7263419]
780 0.154447 [ 0.26137057] [ 0.72978002]
800 0.154393 [ 0.2609086] [ 0.73305506]
820 0.154343 [ 0.26046857] [ 0.7361747]
840 0.154298 [ 0.26004943] [ 0.73914629]
860 0.154257 [ 0.25965014] [ 0.74197692]
880 0.15422 [ 0.25926986] [ 0.74467313]
900 0.154187 [ 0.25890759] [ 0.74724144]
920 0.154156 [ 0.25856245] [ 0.74968821]
940 0.154129 [ 0.25823376] [ 0.75201857]
960 0.154104 [ 0.25792065] [ 0.75423837]
980 0.154081 [ 0.25762239] [ 0.75635284]
1000 0.15406 [ 0.25733829] [ 0.758367]
1020 0.154041 [ 0.25706768] [ 0.76028562]
1040 0.154024 [ 0.25680989] [ 0.76211321]
1060 0.154009 [ 0.25656435] [ 0.76385397]
1080 0.153995 [ 0.25633046] [ 0.76551211]
1100 0.153982 [ 0.25610766] [ 0.76709163]
1120 0.153971 [ 0.25589544] [ 0.76859611]
1140 0.15396 [ 0.25569329] [ 0.77002931]
1160 0.153951 [ 0.25550073] [ 0.77139455]
1180 0.153942 [ 0.2553173] [ 0.77269495]
1200 0.153934 [ 0.25514257] [ 0.77393365]
1220 0.153927 [ 0.25497615] [ 0.77511358]
1240 0.153921 [ 0.25481761] [ 0.77623755]
1260 0.153915 [ 0.25466657] [ 0.77730829]
1280 0.15391 [ 0.25452274] [ 0.77832806]
1300 0.153905 [ 0.25438568] [ 0.77929962]
1320 0.1539 [ 0.25425518] [ 0.78022492]
1340 0.153896 [ 0.25413084] [ 0.78110635]
1360 0.153893 [ 0.25401241] [ 0.781946]
1380 0.15389 [ 0.2538996] [ 0.7827459]
1400 0.153887 [ 0.25379214] [ 0.78350765]
1420 0.153884 [ 0.2536898] [ 0.78423327]
1440 0.153881 [ 0.25359228] [ 0.78492451]
1460 0.153879 [ 0.25349942] [ 0.78558296]
1480 0.153877 [ 0.25341094] [ 0.78621012]
1500 0.153875 [ 0.25332668] [ 0.7868076]
1520 0.153874 [ 0.2532464] [ 0.78737664]
1540 0.153872 [ 0.25316992] [ 0.78791875]
1560 0.153871 [ 0.25309712] [ 0.78843498]
1580 0.15387 [ 0.25302777] [ 0.78892672]
1600 0.153869 [ 0.25296167] [ 0.78939527]
1620 0.153868 [ 0.25289872] [ 0.78984159]
1640 0.153867 [ 0.25283876] [ 0.79026669]
1660 0.153866 [ 0.25278163] [ 0.79067171]
1680 0.153865 [ 0.25272721] [ 0.79105753]
1700 0.153864 [ 0.25267535] [ 0.79142499]
1720 0.153864 [ 0.252626] [ 0.79177499]
1740 0.153863 [ 0.25257897] [ 0.79210842]
1760 0.153863 [ 0.25253418] [ 0.79242599]
1780 0.153862 [ 0.2524915] [ 0.79272854]
1800 0.153862 [ 0.25245085] [ 0.79301667]
1820 0.153861 [ 0.25241214] [ 0.79329121]
1840 0.153861 [ 0.25237525] [ 0.79355264]
1860 0.153861 [ 0.25234014] [ 0.79380178]
1880 0.153861 [ 0.25230667] [ 0.79403901]
1900 0.15386 [ 0.25227478] [ 0.79426497]
1920 0.15386 [ 0.25224444] [ 0.7944802]
1940 0.15386 [ 0.2522155] [ 0.79468524]
1960 0.15386 [ 0.25218797] [ 0.79488051]
1980 0.153859 [ 0.25216171] [ 0.79506654]
2000 0.153859 [ 0.25213674] [ 0.79524374]

In [17]:
#Graphic display
plt.plot(x_train, y_train, 'ro', label='Original data')
plt.plot(x_train, sess.run(W) * x_train + sess.run(b), label='Fitted line')
plt.legend()
plt.show()