[PYTHON] A note about TensorFlow Introduction

While reading TensorFlow Introduction, make a note of what you have investigated.

If you connect all the codes written in Introduction, it will be as follows.

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]

https://www.tensorflow.org/get_started/

Broadcasting operation

y = W * x_data + b
print(type(W)) # <class 'tensorflow.python.ops.variables.Variable'>
print(type(b)) # <class 'tensorflow.python.ops.variables.Variable'>
print(type(x_data)) # <class 'numpy.ndarray'>
print(type(y)) # <class 'tensorflow.python.framework.ops.Tensor'>

I'm scared to multiply tf.Variable and numpy.ndarray here, but Tensorflow seems to support numpy-style broadcasts. For more information, see Stackoverflow and Glossary / glossary) is a good place to look.

Convert numpy to tensor

You can pass numpy.ndarray objects directly to tf.Variable and tf.constant.

import tensorflow as tf
import numpy as np

a = tf.Variable(np.arange(10))
c = (np.zeros(10))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(a)) # [0 1 2 3 4 5 6 7 8 9]
    print(sess.run(c)) # [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

At Quora, TensorFlow team at Google

Yes, the TensorFlow API is designed to make it easy to convert data to and from NumPy arrays:

It states.

Convert tensor to numpy

To convert from tensor to numpy, you can use Session.run or eval.

import tensorflow as tf
import numpy as np

x = tf.zeros([10])

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    print(type(sess.run(x)))
    print(type(x.eval()))

As far as the API documentation is concerned, x.eval () is syntactic sugar for sess.run (x). It looks good to think.

Recommended Posts

A note about TensorFlow Introduction
A note about __call__
A note about subprocess
A note about mprotect (2)
A note about KornShell (ksh)
A note about [python] __debug__
Python: A Note About Classes 1 "Abstract"
A note about get_scorer in sklearn
A note about mock (Python mock library)
[Note] Regarding Tensorflow
Note about awk
Just a note
A note about doing the Pyramid tutorial
A story about simple machine learning using TensorFlow
A note about the python version of python virtualenv
Data analysis in Python: A note about line_profiler
A note about the new style base class
A note about checking modifiers with Max Plus
A memorandum about matplotlib
[TF] About Tensorflow API
A memorandum about Nan.
A quick introduction to pytest-mock
A note about hitting the Facebook API with the Python SDK
An introduction to private TensorFlow
A super introduction to Linux
A memorandum about correlation [Python]
A memorandum about Python mock
A little more about FIFO
A small note following printf
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
A note about connecting Spark to OpenStack Swift-based IBM Object Storage
I made a Dir en gray face classifier using TensorFlow --(1) Introduction
Python Note: About comparison using is
Introduction to machine learning Note writing
A refreshing story about Python's Slice
Introduction to TensorFlow --Hello World Edition
A note when gcloud is broken
A sloppy story about Python's Slice
I have a question about whitespace
A small sample note of list_head
A note for writing Python-like code
Run TensorFlow2 on a VPS server
A light introduction to object detection
A note that prints numpy.array nicely
A story about using Python's reduce
[Translation] A note about structured concurrency .. or rather go statements seem harmful
(Note) A web application that uses TensorFlow to infer recommended song names.
[Just a note] Until Keras + TensorFlow works on Mac OS X Sierra
A note about the functions of the Linux standard library that handles time