I tried the TensorFlow tutorial 1st

I tried TensorFlow Official Tutorial It's just a simple calculation graph. Make sure you have TensorFlow installed.

Getting Started with TensorFlow

Run python in the terminal (hereafter in the interpreter)

Tensorflow import

>>>import tensorflow as tf

Calculation graph

>>>node1 = tf.constant(3.0, dtype=tf.float32) #Constant 3.Set to 0
>>>node2 = tf.constant(4.0)
>>>print(node1, node2)

Nodes generate 3.0 and 4.0 when evaluated. To actually evaluate a node, you need to run a computational graph within the session.

print output


Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

Create a Session object and execute the calculation graph (node1, node2) to evaluate by calling the run method.

>>>sess = tf.Session()
>>>print(sess.run([node1, node2]))

print output


[3.0, 4.0]

A complex calculation (new calculation graph) is created by combining these nodes (node1, node2). Add (create addition) for the time being

>>>node3 = tf.add(node1, node2) # node1 + node2
>>>print("node3:", node3)
>>>print("sess.run(node3):", sess.run(node3))

print output


node3: Tensor("Add:0", shape=(), dtype=float32)
sess.run(node3): 7.0

Since constants are set in this graph as it is, use placeholder to accept external input.

>>>a = tf.placeholder(tf.float32)
>>>b = tf.placeholder(tf.float32)
>>>adder_node = a + b  # + provides a shortcut for tf.add(a, b)
>>>print(sess.run(adder_node, {a: 3, b: 4.5}))
>>>print(sess.run(adder_node, {a: [1, 3], b: [2, 4]}))

print output


7.5
[ 3.  7.]

Let's make the calculation graph more complicated.

>>>add_and_triple = adder_node * 3.
>>>print(sess.run(add_and_triple, {a: 3, b: 4.5}))

When I actually convert it into a mathematical formula, it does the following calculation. (a + b) * 3

print output


22.5

Machine learning needs to be able to modify the graph to get new output with the same input. Variables allow you to add trainable parameters to your graph.

When tf.constant is called, it is initialized and a constant is set, so it cannot be changed. tf.Variable can be updated without initializing the variable even if the variable is called.

>>>W = tf.Variable([.3], dtype=tf.float32)
>>>b = tf.Variable([-.3], dtype=tf.float32)
>>>x = tf.placeholder(tf.float32)
>>>linear_model = W * x + b
>>>init = tf.global_variables_initializer()
>>>sess.run(init)
>>>print(sess.run(linear_model, {x: [1, 2, 3, 4]}))

ʻInit = .. ~ is a variable for initializing all variables in the TensorFlow program. Variables are not initialized until you call sess.run (init)`

print output


[ 0.          0.30000001  0.60000002  0.90000004]

We need a placeholder to provide the value of the desired value (teacher data), and we also need a loss function.

Loss function

Measures how far the value output by the current model is from the teacher data. Use the standard loss model for linear regression of the values output by the model and the teacher data. linear_model --y calculates the vector of the error that each element tyers, and squares the error with tf.square. Then call tf.reduce_sum to generate a single scalar that abstracts all the errors.

>>>y = tf.placeholder(tf.float32)
>>>squared_deltas = tf.square(linear_model - y)
>>>loss = tf.reduce_sum(squared_deltas)
>>>print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

The output of print returns the value of the loss function.

print output


23.66

{x: [1, 2, 3, 4], y: [0, -1, -2, -3]} Looking at the two values of the input, when W = -1 b = 1 The loss is likely to be zero. You can use tf.assign to change the weights and biases.

>>>fixW = tf.assign(W, [-1.])
>>>fixb = tf.assign(b, [1.])
>>>sess.run([fixW, fixb])
>>>print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

print output


0.0

Recommended Posts

I tried the TensorFlow tutorial 1st
I tried the TensorFlow tutorial 2nd
I tried the TensorFlow tutorial MNIST 3rd
I tried the MNIST tutorial for beginners of tensorflow.
I tried TensorFlow tutorial CNN 4th
I tried tensorflow for the first time
I tried running TensorFlow
I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
I tried the changefinder library!
I tried using magenta / TensorFlow
I tried running the TensorFlow tutorial with comments (text classification of movie reviews)
I tried porting the code written for TensorFlow to Theano
I tried to find the average of the sequence with TensorFlow
I tried the Naro novel API 2
I tried refactoring the CNN model of TensorFlow using TF-Slim
I tried the Naruro novel API
TensorFlow tutorial tutorial
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
[For beginners] I tried using the Tensorflow Object Detection API
I tried the asynchronous server of Django 3.0
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried playing a ○ ✕ game using TensorFlow
I tried the OSS visualization tool, superset
I tried to classify text using TensorFlow
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
Python: I tried the traveling salesman problem
I tried playing with the image with Pillow
I tried the Python Tornado Testing Framework
I tried using the BigQuery Storage API
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried scraping
I tried PyQ
I tried AutoKeras
I tried papermill
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried web scraping to analyze the lyrics.
I tried using scrapy for the first time
I tried the pivot table function of pandas
[Python] I tried substituting the function name for the function name
I tried cluster analysis of the weather map
I tried hitting the Qiita API from go
vprof --I tried using the profiler for Python
I tried "differentiating" the image with Python + OpenCV
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I tried non-negative matrix factorization (NMF) with TensorFlow
I tried using PyCaret at the fastest speed
Before the coronavirus, I first tried SARS analysis
I tried using the Google Cloud Vision API