[DOCKER] I tried to find an alternating series with tensorflow

I recently started studying tensorflow, so I asked for an alternating series. If you make a mistake, please comment.

Purpose

\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)

Execution environment

Execution method

$ 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.

Implementation

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 ")

Description

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 of y 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.

result

Image result

result.png

Output result to terminal

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

Finally

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!

References

the Internet

-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

Books

-[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

I tried to find an alternating series with tensorflow
I tried to find the average of the sequence with TensorFlow
I tried to implement Autoencoder with TensorFlow
I tried to visualize AutoEncoder with TensorFlow
I tried to detect an object with M2Det!
I tried to implement time series prediction with GBDT
I tried to implement Grad-CAM with keras and tensorflow
I tried to make an OCR application with PySimpleGUI
I tried to find the entropy of the image with python
I tried to create an article in Wiki.js with SQLAlchemy
I tried sending an SMS with Twilio
I tried to get started with Hy
I tried sending an email with python.
I tried to classify text using TensorFlow
I tried to find out if ReDoS is possible with Python
I tried to implement CVAE with PyTorch
I tried to solve TSP with QAOA
I tried to make an image similarity function with Python + OpenCV
I tried to make an open / close sensor (Twitter cooperation) with TWE-Lite-2525A
I tried to predict next year with AI
I tried to detect Mario with pytorch + yolov3
I tried to implement reading Dataset with PyTorch
I tried to learn logical operations with TF Learn
I tried to move GAN (mnist) with keras
I tried to get an image by scraping
I tried to save the data with discord
I tried to detect motion quickly with OpenCV
I tried to integrate with Keras in TFv1.1
I want to be an OREMO with setParam!
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to automate sushi making with python
I tried to predict Titanic survival with PyCaret
I tried to operate Linux with Discord Bot
I tried to study DP with Fibonacci sequence
I tried to start Jupyter with Amazon lightsail
I tried to judge Tsundere with Naive Bayes
I tried to make a ○ ✕ game using TensorFlow
I tried to debug.
I tried to paste
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I tried to extract a line art from an image with Deep Learning
I tried to make a simple image recognition API with Fast API and Tensorflow
I tried to learn the sin function with chainer
I want to convert an image to WebP with lollipop
I tried to move machine learning (ObjectDetection) with TouchDesigner
I tried to create a table only with Django
I tried to extract features with SIFT of OpenCV
I tried to move Faster R-CNN quickly with pytorch
I tried to read and save automatically with VOICEROID2 2
I tried to implement and learn DCGAN with PyTorch
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried Smith standardizing an integer matrix with Numpy
I tried to get started with blender python script_Part 02
I tried to generate ObjectId (primary key) with pymongo
I tried to get an AMI using AWS Lambda
I tried to become an Ann Man using OpenCV
I tried to uncover our darkness with Chatwork API