【TensorFlow学习小组】Week 1动手任务list

作业

#1

####2017.11.1 ####动手任务: 1、使用tf计算下面算式的值: x=2 ,y=3,z=7 求解:res=x*y+z的结果 2、使用tf计算求解,矩阵乘法结果: 矩阵乘法: A :[[3., 3.]] B: [[2.],[2.]] A矩阵和B矩阵的乘法运算 ####备注: 代码结果在后面帖子回复


#2
x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)

mul = tf.multiply(x, y)
res = tf.add(mul, z)

with tf.Session() as sess:
 final = sess.run(res)
#

mat1 = tf.constant([[3.0, 3.0]])
mat2 = tf.constant([[2.0], [2.0]])

mul = tf.matmul(mat1, mat2)

with tf.Session() as sess:
 final  = sess.run(mul)

#3
import tensorflow as tf
import numpy as np

#1

x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)
res = x * y + z
with tf.Session() as sess:
    print(sess.run(res))

#2

A = tf.constant(
    [[3., 3.]]
)
B = tf.constant(
    [[2.],
     [2.]]
)
out = tf.matmul(A, B)
with tf.Session() as sess:
    print(sess.run(out))

#4

我来个“啰嗦”的写法

import tensorflow as tf

x = tf.constant(2, name='x')
y = tf.constant(3, name='y')
z = tf.constant(7, name='z')
mul_x_y = tf.multiply(x, y, name='x_mul_y')
xy_add_z = tf.add(mul_x_y, z, name='result_num')
sess = tf.Session()
print(sess.run(xy_add_z))

A = tf.placeholder(tf.float32, [1, 2], name='mat_A')
B = tf.placeholder(tf.float32, [2, 1], name='mat_B')
C = tf.matmul(A, B, name='result_mat') 
print(sess.run(C, feed_dict={A: [[3., 3.]], B: [[2.], [2.]]}))

注:不知道刚刚接触tensorflow的小伙伴会不会跟我一样,矩阵乘法直接写A*B。tensorflow中 * 等同于multiply,是元素相乘。


#5

1

import tensorflow as tf

x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)
'''
m = tf.multiply(x, y)
res = tf.add(m, z)
'''
with tf.Session() as sess:
    res = sess.run((x * y) + z)
    print(res)

2

A = tf.constant([[3.0, 3.0]])
B = tf.constant([[2.0], [2.0]])

with tf.Session() as sess:
    final  = sess.run(tf.matmul(A, B))
    print(final)

#6

##1

x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)
sess = tf.Session()
res = x * y + z
print(sess.run(res))

##2

import tensorflow as tf

mat1 = tf.constant([[3.,3.]])
mat2 = tf.constant([[2.],[2.]])

product = tf.matmul(mat1, mat2)

with tf.Session() as sess:
    result = sess.run(product)
    print(result)

#7
import tensorflow as tf
x=tf.constant(2)
y=tf.constant(3)
z=tf.constant(7)
oprate1=tf.add(tf.multiply(x,y),z)
A=tf.constant([[3.,3.]])
B=tf.constant([[2.],[2.]])
oprate2=tf.matmul(A,B)
# sess = tf.Session()
with tf.Session() as sess:
    res1=sess.run(oprate1)
    res2 = sess.run(oprate2)

#8
import tensorflow as tf

x=tf.constant(2)
y=tf.constant(3)
z=tf.constant(7)
res=x*y+z
with tf.Session() as sess:
    print (sess.run(res))

import tensorflow as tf
a=tf.constant([[3.,3.]])
b=tf.constant([[2.],[2.]])
res1=tf.matmul(a,b)
with tf.Session() as sess:
    print(sess.run(res1))


#9

刚开始学,对着前辈们的写的。 第一题:


#10

第二题:


#11


#12

1、使用tf计算下面算式的值: x=2 ,y=3,z=7 求解:res=x*y+z的结果

2、使用tf计算求解,矩阵乘法结果: 矩阵乘法: A :[[3., 3.]] B: [[2.],[2.]] A矩阵和B矩阵的乘法运算


#13


#14
import tensorflow as tf
x=tf.constant(2)
y=tf.constant(3)
z=tf.constant(7)
oprate1=tf.add(tf.multiply(x,y),z)
A=tf.constant([[3.,3.]])
B=tf.constant([[2.],[2.]])
oprate2=tf.matmul(A,B)
# sess = tf.Session()
with tf.Session() as sess:
    res1=sess.run(oprate1)
    res2 = sess.run(oprate2)
print(res1)
print(res2)

#15
import tensorflow as tf

x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)

A = tf.constant([[3.,3.]]) # A: 1X2 matrix
B = tf.constant([[2,],[2.]]) # B: 2X1 matrix

with tf.Session() as sess:
    res1 = sess.run(x * y + z)
    res2 = sess.run(tf.matmul(A, B))

print(res1) # res1 = 13
print(res2) # res2 = [[ 12.]]

#16
import tensorflow as tf
sess = tf.Session()

# 任务1
x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)
res = x * y + z
print(sess.run(res))   # 13

# 任务2
A = tf.constant([[3., 3.]])
B = tf.constant([[2.], [2.]])
print(sess.run( tf.matmul(A, B)))  # [[ 12.]]

sess.close()

#17


#18


#19
import tensorflow as tf
import numpy as np 

1

x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)
result = x*y + z
with tf.Session() as sess:
    print(sess.run(res))

2

A = tf.constant([[3.,3.]])
B = tf.constant([[2.],[2.]])
out = tf.matmul(A,B)
with tf.Session() as sess:
    print(sess.run(out))

#20
import tensorflow as tf
sess = tf.Session()
x = tf.constant(2)
y = tf.constant(3)
z = tf.constant(7)
 sess.run(x*y+z)

mat1 = tf.constant([[3.,3.]])
mat2 = tf.constant([[2.],[2.]])
out = tf.matmul(mat1,mat2)
ress.run(out)