0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Tensorflowを使って足し算から始める

Last updated at Posted at 2019-02-04

インポート

import tensorflow as tf

定数と変数を作ってみる

a = tf.constant(3, name='const1')#定数: 一度決めたら後で変更できない
b = tf.Variable(0, name='val1')#変数: 値を変更できる

計算グラフを作ってみる

tf.add(a,b)
# Tensor("Add:0", shape=(), dtype=int32)

ここでは計算グラフが定義されただけで、printするとオブジェクトが返ってきてしまう。

実行

# 変数の初期化
init = tf.global_variables_initializer()

with tf.Session() as sess:
  #初期化を実行
  sess.run(init)
  #足し算を実行
  print(sess.run(add))

変数を使うときは計算グラフを実行する前に、変数を初期化してあげなければならない。
定数を使うときは、初期化しなくても問題ない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?