I recently started studying tensorflow, so I asked for an alternating series. If you make a mistake, please comment.
\sum^{\infty}_{n=1}(-1)^{n-1}/n\ \But\log{2}\I want to know if it converges to.
This formula can be proved by using the quadrature method. (See Resources for details)
$ docker run -v [Directory with code you want to run on windows]:/app -it b.gcr.io/tensorflow/tensorflow:latest-devel bash
# cd /app
# python alternating_sum.py
You can share the container file with the host with the -v option. For more information, please read Docker Official.
alternating_sum.py
# coding:utf-8
import tensorflow as tf
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
N = 50000
#For graphs
figy = np.zeros([21])
figdif = np.zeros([21])
#Settings such as variables
y = tf.Variable(tf.cast(0, tf.float32))
x = tf.placeholder(tf.float32)
#operation
update_y = tf.assign(y, tf.add(y, tf.div(tf.cast(1, tf.float32), x)))
#Error with log2
dif = tf.sub(tf.log(tf.cast(2, tf.float32)), y)
#session initialization
sess = tf.Session()
sess.run(tf.initialize_all_variables())
#Start calculation
i = 0
for _ in range(N):
i += 1
tmp = -i if i % 2 == 0 else i
sess.run(update_y, feed_dict={x: tmp})
if i <= 20:
figy[i] = sess.run(y)
figdif[i] = sess.run(dif)
if i % 10 == 0:
print('STEP: %d, RES: %f, DIF: %f' %(i, sess.run(y), sess.run(dif)))
#Creating a graph
plt.plot(figy, label = "result")
plt.plot(figdif, label = "dif")
plt.legend()
plt.ylim([-2,2])
plt.savefig("result.png ")
y = tf.Variable(tf.cast(0, tf.float32))
The initial variable is 0. Without tf.cast, it will cause a TypeError.
x = tf.placeholder(tf.float32)
x can be a value such as
1
or-2
.
update_y = tf.assign(y, tf.add(y, tf.div(tf.cast(1, tf.float32), x)))
Find 1 / x with tf.div and y + tf.div with tf.add. It will be one line that assigns that value to y with tf.assign. That is,
y = y + 1 / x
. Think of update_y as the operator that does that.
dif = tf.sub(tf.log(tf.cast(2, tf.float32)), y)
The difference between log2 and y is taken. The value of log2 uses the function of tensorflow.
sess.run(tf.initialize_all_variables())
Initializing each variable.
for statement
You will actually do the calculations in sess.run. If you pass a variable such as
y
as an argument here, the current value ofy
will be returned. I am trying to put a value in x declared earlier in feed_dict.
From the above, we are looking for the result of the desired formula. Read Resources for graphs.
Slightly modified to make it easier to see. I haven't messed with the numbers.
STEP: 1, RES: 1.000000, DIF: -0.306853
STEP: 2, RES: 0.500000, DIF: 0.193147
STEP: 3, RES: 0.833333, DIF: -0.140186
STEP: 4, RES: 0.583333, DIF: 0.109814
STEP: 5, RES: 0.783333, DIF: -0.090186
STEP: 6, RES: 0.616667, DIF: 0.076481
STEP: 7, RES: 0.759524, DIF: -0.066377
STEP: 8, RES: 0.634524, DIF: 0.058623
STEP: 9, RES: 0.745635, DIF: -0.052488
STEP: 10, RES: 0.645635, DIF: 0.047512
STEP: 100, RES: 0.688172, DIF: 0.004975
STEP: 1000, RES: 0.692646, DIF: 0.000501
STEP: 10000, RES: 0.693092, DIF: 0.000055
STEP: 50000, RES: 0.693129, DIF: 0.000018
I think that tensorflow is often used in deep running etc., but this time I did a simple numerical calculation. There were some stumbling blocks, but I enjoyed making it with tensorflow. tensorflow is good!
-Introduction to TensorFlow --Summary of four arithmetic operations and basic mathematical functions -Understanding TensorFlow by Arithmetic -Tensorflow Official Document -Proof of alternating series that converges on log2 -Introduction to matplotlib -Meeting Python for Machine Learning -Data science with Python
-[TensorFlow started practice! Latest Google Machine Learning (Next Publishing)](https://www.amazon.co.jp/TensorFlow%E3%81%AF%E3%81%98%E3%82%81%E3%81%BE%E3%81 % 97% E3% 81% 9F-% E5% AE% 9F% E8% B7% B5% EF% BC% 81% E6% 9C% 80% E6% 96% B0Google% E3% 83% 9E% E3% 82% B7% E3% 83% B3% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0-NextPublishing-% E6% 9C% 89% E5% B1 % B1-% E5% 9C% AD% E4% BA% 8C-ebook / dp / B01IT509EY) -[Introduction to deep learning learned with TensorFlow-Thorough explanation of convolutional neural network](https://www.amazon.co.jp/TensorFlow%E3%81%A7%E5%AD%A6%E3%81%B6%E3%] 83% 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0% E5% 85% A5% E9% 96% 80% EF% BD% 9E% E7% 95% B3% E3% 81% BF% E8% BE% BC% E3% 81% BF% E3% 83% 8B% E3% 83% A5% E3% 83% BC% E3% 83% A9% E3% 83% AB% E3% 83% 8D% E3% 83% 83% E3% 83% 88% E3% 83% AF% E3% 83% BC% E3% 82% AF% E5% BE% B9% E5% BA% 95% E8% A7% A3% E8% AA% AC-% E4% B8% AD% E4% BA% 95-% E6% 82% A6% E5% 8F% B8-ebook / dp / B01MAWJJOW / ref = sr_1_2? s = digital-text & ie = UTF8 & qid = 1479886851 & sr = 1-2 & keywords = tensorflow)
Recommended Posts