In [1]:
import tensorflow as tf
from keras import backend as K
In [9]:
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
with tf.Session() as sess:
# add this line to use TensorBoard.
writer = tf.summary.FileWriter('./graphs', sess.graph)
print (sess.run(x))
writer.close()
In [14]:
a = tf.constant(2, name="c")
b = tf.constant(3, name="d")
x = tf.add(a, b, name="add")
writer = tf.summary.FileWriter("./graphs", sess.graph)
with tf.Session() as sess:
print (sess.run(x)) # >> 5
In [8]:
a = tf.constant([1, 2,3], name="a")
b = tf.constant([[0, 1, 2], [3, 4,5]], name="b")
x = tf.add(a, b, name="add1")
y = tf.add(b, a, name="add2")
z = tf.multiply(a, b, name="mul1")
w = tf.multiply(b, a, name="mul2")
with tf.Session() as sess:
x, y ,z,w = sess.run([x, y, z, w])
print("a+b:", x)
print("b+a:", y)
print("a*b:", z)
print("b*a:", w)
In [24]:
def repeat(x, num_repeats):
ones = tf.ones((1, num_repeats), dtype='int32')
x = tf.reshape(x, shape=(-1,1))
x = tf.matmul(x, ones)
return tf.reshape(x, [-1])
a = tf.range(32, name="a")
b = tf.constant(3600, name="b")
y = repeat(a*b, 30*30)
with tf.Session() as sess:
x = sess.run(y)
print(x)
print(x.shape)
# 0,..(900 times)..,0,3600*1,..(900 times)..,3600*1,3600*2,....,3600*2, ......., 3600*31
In [15]:
# gather 蒐集某 index 以下所有的值,並保留維度
a = tf.constant(2, name="a")
b = tf.constant([[1,2,3],[4,5,6],[7,8,4],[3,2,3],[1,1,23]], name="b")
c = tf.constant([1,2], name="c")
y = tf.gather(b, a)
z = tf.gather(b, c)
with tf.Session() as sess:
x,w,f = sess.run([y,z,k])
print(x)
print(z)
print(z.eval())
In [16]:
a = tf.constant([1,2,3,4,5,6,7,8,9,10], name="a")
b = tf.constant([1,3,3,3,5,3,7,3,9,3], name="b")
c = tf.constant([1,2,3,4,5,6,7,8,9,10], name="c")
d = tf.constant([2,2,3,4,1,6,7,1,9,2], name="d")
area_a = tf.expand_dims((b-a)*(d-c), 1)
area_b = tf.expand_dims((b-a)*(c-d), 1)
y = tf.add_n([area_a, area_b])
with tf.Session() as sess:
x = sess.run(y)
print(x)
print(area_a)
print(area_b)
In [6]:
def meshgrid(height, width):
x_linspace = tf.linspace(-1., 1., width)
y_linspace = tf.linspace(-1., 1., height)
x_coordinates, y_coordinates = tf.meshgrid(x_linspace, y_linspace)
x_coordinates = tf.reshape(x_coordinates, [-1])
y_coordinates = tf.reshape(y_coordinates, [-1])
ones = tf.ones_like(x_coordinates)
indices_grid = tf.concat([x_coordinates, y_coordinates, ones], 0)
return indices_grid
aff = tf.constant([[1,2,3,4,5,6],[-.1,-.2,-.3,-.4,-.5,-.6]])
aff = tf.reshape(aff, shape=(2,2,3))
aff = tf.reshape(aff, (-1, 2, 3))
aff = tf.cast(aff, 'float32')
a = tf.constant(5, name="a")
b = meshgrid(a,a)
c = tf.expand_dims(b, 0)
d = tf.reshape(c, [-1])
e = tf.tile(d, tf.stack([2]))
f = tf.reshape(e, (2, 3, -1))
res = tf.matmul(aff, f)
x_s = tf.slice(res, [0, 0, 0], [-1, 1, -1])
y_s = tf.slice(res, [0, 1, 0], [-1, 1, -1])
with tf.Session() as sess:
b,c,d,e,f,aff,res,x_s,y_s = sess.run([b,c,d,e,f,aff, res,x_s,y_s])
print('b',b.shape)
print(b)
# print('c',c.shape)
# print(c)
# print('d',d.shape)
# print(d)
# print('e',e.shape)
# print(e)
print('meshgrid with batch',f.shape)
print(f)
print('aff',aff.shape)
print(aff)
print('new coor',res.shape)
print(res)
print('new x',x_s.shape)
print(x_s)
print('new y',y_s.shape)
print(y_s)
In [8]:
def meshgrid(height, width):
x_linspace = tf.linspace(-1., 1., width)
y_linspace = tf.linspace(-1., 1., height)
x_coordinates, y_coordinates = tf.meshgrid(x_linspace, y_linspace)
x_coordinates = tf.reshape(x_coordinates, [-1])
y_coordinates = tf.reshape(y_coordinates, [-1])
# ones = tf.ones_like(x_coordinates)
indices_grid = tf.concat([x_coordinates, y_coordinates], 0)
return indices_grid
aff_ori = tf.constant([[[1,2,3,4,5,6,7,8,9],[-.1,-.2,-.3,-.4,-.5,-.6,-.7,-.8,-.9]],
[[10,10,30,40,50,60,70,80,90],[-.01,-.02,-.03,-.04,-.05,-.06,-.07,-.08,-.09]]])
aff = tf.reshape(aff, shape=(2,2,-1))
aff = tf.cast(aff, 'float32')
a = tf.constant(3, name="a")
b = meshgrid(a,a)
c = tf.tile(b, tf.stack([2]))
d = tf.reshape(c, (2, 2, -1))
res = tf.add(aff, d)
x_s = tf.slice(res, [0, 0, 0], [-1, 1, -1])
y_s = tf.slice(res, [0, 1, 0], [-1, 1, -1])
with tf.Session() as sess:
aff_ori,b,c,d,aff,res,x_s,y_s = sess.run([aff_ori,b,c,d,aff,res,x_s,y_s])
# print(b.shape)
# print(b)
# print(c.shape)
# print(c)
print('flow_ori',aff_ori.shape)
print('meshgrid with batch',d.shape)
print(d)
print('flow',aff.shape)
print(aff)
print('new coor',res.shape)
print(res)
print('new x',x_s.shape)
print(x_s)
print('new y',y_s.shape)
print(y_s)
In [8]:
def mean_squared_error(y_true, y_pred):
# return K.mean(K.square(y_pred - y_true), axis=-1)
return K.mean(K.sqrt(K.square(y_pred - y_true)), axis=-1)
a = tf.constant([[[[1.,2.],[4.,5.],[7.,8.]],
[[1.,3.],[4.,6.],[8.,9.]],
[[1.,3.],[4.,6.],[7.,8.]]],
[[[1.,3.],[4.,5.],[7.,9.]],
[[1.,2.],[4.,5.],[7.,9.]],
[[1.,3.],[4.,5.],[7.,9.]]],
[[[2.,3.],[4.,6.],[7.,9.]],
[[2.,3.],[4.,6.],[7.,9.]],
[[1.,3.],[4.,6.],[7.,9.]]]], name="a")
b = tf.constant([[[[2.,3.],[5.,7.],[8.,10.]],
[[2.,4.],[6.,7.],[8.,10.]],
[[3.,4.],[5.,6.],[9.,10.]]],
[[[1.,3.],[5.,7.],[8.,10.]],
[[3.,4.],[5.,7.],[9.,10.]],
[[2.,3.],[5.,6.],[8.,10.]]],
[[[2.,3.],[6.,7.],[8.,9.]],
[[3.,4.],[6.,7.],[8.,9.]],
[[2.,4.],[6.,7.],[8.,9.]]]], name="b")
a = tf.transpose(a,[0,3,2,1])
b = tf.transpose(b,[0,3,2,1])
x = mean_squared_error(a,b)
with tf.Session() as sess:
a,b,x = sess.run([a,b,x])
print(x)
a.shape
Out[8]: