!nvidia-smi

import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
#config.gpu_options.per_process_gpu_memory_fraction = 0.01


"""
Explanation: Tensorflow
Graph = Operations + Tensors
Session runs a graph on a device(s).
Tensorflow na  GPU -
praktyczne uwagi pracy na tym samym systemie
https://www.tensorflow.org/tutorials/using_gpu
Limit memory:
- config.gpu_options.allow_growth = True
- config.gpu_options.per_process_gpu_memory_fraction = 0.01

Uwaga: GPU resources not released when session is closed https://github.com/tensorflow/tensorflow/issues/1727
Trzeba zakończyć proces pythona (w jupyter: kernel restart)
Aby sprawdzić zasoby GPU: nvidia-smi
End of explanation
"""


sess = tf.InteractiveSession(config=config)
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
x.initializer.run()

sub = tf.subtract(x, a)
print(sub.eval())

sess.close()

sess = tf.Session(config=config)
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
sess.run(x.initializer) # różnica 1

sub = tf.subtract(x, a)
print(sess.run(sub))   # różnica 2

sess.close()

"""
Explanation: Interactive TensorFlow Session.

t.eval() is a shortcut for calling tf.get_default_session().run(t)
End of explanation
"""


with  tf.Session(config=config) as sess:
    with tf.device("/gpu:0"):
        x = tf.Variable([1.0, 2.0])
        a = tf.constant([3.0, 3.0])
        x.initializer.run()

        sub = tf.subtract(x, a)
        print(sub.eval())



with  tf.Session(config=config) as sess:
    with tf.device("/gpu:0"):
        x = tf.Variable([1.0, 2.0])
        a = tf.constant([3.0, 3.0])
        sess.run(x.initializer) # różnica 1
        
        sub = tf.subtract(x, a)
        print(sess.run(sub))   # różnica 2



"""
Explanation: context manager
End of explanation
"""


c = tf.constant(4.0)
c.graph is tf.get_default_graph()



with  tf.Session(config=config) as sess:
    with tf.device("/gpu:0"):
        x = tf.Variable([1.0, 2.0])
        a = tf.constant([3.0, 3.0],name="A")
        sub = tf.subtract(x, a)
        sess.run(x.initializer) 
        
        writer = tf.summary.FileWriter("/DATA/marcin.kostur/tf001",sess.graph)
        writer.close()
        
        sub = tf.subtract(x, a)
        print(sess.run(sub))   


"""
Explanation: Graphs
End of explanation
"""


!ls -l   /DATA/marcin.kostur/tf001/* 

!tensorboard --logdir=/DATA/marcin.kostur/tf001/ --port 4001

tf.

"""
Explanation: Tensorboard
https://www.tensorflow.org/get_started/summaries_and_tensorboard
Uwaga - należy wybrać unikalny port: --port 3456
End of explanation
"""


import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.reset_default_graph()

sess = tf.InteractiveSession(config=config)

a = tf.linspace(0.,11.,23)


b =  a*a
print(b.eval())

c = tf.constant([1,2,3.])

type(a)

"""
Explanation: Constant, variables, placeholders
Constants:
tf.constant

tf.zeros
tf.zeros_like
tf.ones
tf.ones_like
tf.fill
tf.linspace
tf.range
End of explanation
"""


x = tf.Variable([1,2,3.])

type(x)

x.initializer.run()
x.eval()

(a[:3]*x)

(a[:3]*x).eval()

"""
Explanation: Variables
End of explanation
"""


y = tf.placeholder(tf.float32)

d = y*a

d.eval(feed_dict={y:1.23})

"""
Explanation: Placeholders
End of explanation
"""


n = tf.random_normal([2,2])

n.eval()

n = tf.random_uniform([12])

n.eval()

sess.close()

"""
Explanation: Random numbers
End of explanation
"""


