Do not learn with machine learning library TensorFlow ~ Fibonacci sequence

For those who are not familiar with machine learning but want to calculate within the range that they can understand, let's try Fibonacci sequence with TensorFlow.

Basic

Fibonacci sequence $ a_{n+2} = a_{n+1} + a_n, \\ a_1 = 1, \\ a_0 = 0 $

For example, to calculate


u = 1
v = 0
for i in range(100):
	print i, v 
	u = u + v
	v = u - v

You can do it like this.

Preparation

TensorFlow

import tensorflow as tf

Suppose you want to read with.

Variable First, let's change the variables u and v to TensorFlow variables. To do this, use Variable as follows:

u = tf.Variable(1, "int64")
v = tf.Variable(0, "int64")

I chose int64 as an overflow countermeasure, but unfortunately, an overflow occurs in Section 93. If this is omitted, it will be treated as int32.

display

It sounds like you can print it with print u, but unfortunately you won't get what you expect. TensorFlow is divided into a phase of defining variables, functions and procedures, and a phase of actually processing data.

Use tf.Session to actually process the data. Variables need to be initialized at the beginning of the session. Use tf.initialize_all_variables for this. And you can get the values of u and v by doing the following.

init = tf.initialize_all_variables()
with tf.Session() as sess:
	sess.run(init)
	print sess.run(v)

Take values and execute procedures with sess.run.

update

The rest is the value update part. Use tf.assign for assignment. Use tf.add and tf.sub for addition and subtraction. And let's define a procedure called substitution.

update_u = tf.assign(u, tf.add(u, v)) # u = u + v
update_v = tf.assign(v, tf.sub(u, v)) # v = u - v

Summary

To summarize the above story,

import tensorflow as tf

u = tf.Variable(tf.cast(1,"int64"))
v = tf.Variable(tf.cast(0,"int64"))

update_u = tf.assign(u, tf.add(u,v))
update_v = tf.assign(v, tf.sub(u,v))

init = tf.initialize_all_variables()

with tf.Session() as sess:
	sess.run(init)
	for i in range(100):
		print i, sess.run(v)
		sess.run(update_u)
		sess.run(update_v)

When I run it, somehow the CPU somehow displays a log that starts with I, but since it is Info, I don't care.

If you haven't touched TensorFlow yet, let's do something.

Recommended Posts

Do not learn with machine learning library TensorFlow ~ Fibonacci sequence
Install the machine learning library TensorFlow on fedora23
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
Somehow learn machine learning
Machine learning library Shogun
Learn collaborative filtering along with Coursera Machine Learning materials
Installation of TensorFlow, a machine learning library from Google
Site summary to learn machine learning with English video
Machine learning learned with Pokemon
Try deep learning with TensorFlow
Machine learning with Python! Preparation
For those who want to start machine learning with TensorFlow2
Machine learning Minesweeper with PyTorch
Machine learning to learn with Nogizaka46 and Keyakizaka46 Part 1 Introduction
Beginning with Python machine learning
Try machine learning with Kaggle
A story stuck with the installation of the machine learning library JAX
[Reading Notes] Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow Chapter 1
Do not switch with pyenv global!
I tried machine learning with liblinear
Machine learning with python (1) Overall classification
[TensorFlow 2] Learn RNN with CTC Loss
Try deep learning with TensorFlow Part 2
Try machine learning with scikit-learn SVM
<For beginners> python library <For machine learning>
Introduction to Machine Learning Library SHOGUN
Quantum-inspired machine learning with tensor networks
Get started with machine learning with SageMaker
"Scraping & machine learning with Python" Learning memo
I tried to visualize the model with the low-code machine learning library "PyCaret"
[Memo / Creating] "Hands-On Machine Learning with Scikit-Learn & TensorFlow" English translation into Japanese
Algorithm learned with Python 5th: Fibonacci sequence
Predict power demand with machine learning Part 2
[Reinforcement learning] DQN with your own library
Learn data distributed with TensorFlow Y = 2X
Amplify images for machine learning with python
Machine learning imbalanced data sklearn with k-NN
Machine learning with python (2) Simple regression analysis
TensorFlow Machine Learning Cookbook Chapter 2 Personally Clogged
A story about machine learning with Kyasuket
[Shakyo] Encounter with Python for machine learning
I tried recursion with Python ② (Fibonacci sequence)
Calculate Fibonacci sequence with generator and iterator
Machine learning with Pytorch on Google Colab
TensorFlow Machine Learning Cookbook Chapter 3 Personally Clogged
Build AI / machine learning environment with Python
[Machine learning] Regression analysis using scikit learn
Chapter 6 Supervised Learning: Classification pg212 ~ [Learn by moving with Python! New machine learning textbook]