Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. Lets start this programming exercise by computing the following equation: Y=WX+b , where W and X are random matrices and b is a random vector.
  2.  
  3. Exercise: Compute WX+bW where W,X , and b are drawn from a random normal distribution. W is of shape (4, 3), X is (3,1) and b is (4,1). As an example, here is how you would define a constant X that has shape (3,1):
  4.  
  5. X = tf.constant(np.random.randn(3,1), name = "X")
  6. You might find the following functions helpful:
  7.  
  8. tf.matmul(..., ...) to do a matrix multiplication
  9. tf.add(..., ...) to do an addition
  10. np.random.randn(...) to initialize randomly
  11.  
  12. def linear_function():
  13. """
  14. Implements a linear function:
  15. Initializes W to be a random tensor of shape (4,3)
  16. Initializes X to be a random tensor of shape (3,1)
  17. Initializes b to be a random tensor of shape (4,1)
  18. Returns:
  19. result -- runs the session for Y = WX + b
  20. """
  21.  
  22. np.random.seed(1)
  23.  
  24. ### START CODE HERE ### (4 lines of code)
  25. X = tf.constant(np.random.randn(3,1), name = X)
  26. W = tf.constant(np.random.randn(4,3), name = W)
  27. b = tf.constant(np.random.randn(4,1), name = b)
  28. Y = tf.constant(tf.add(tf.matmul(W, X), b), name = Y)
  29. ### END CODE HERE ###
  30.  
  31. # Create the session using tf.Session() and run it with sess.run(...) on the variable you want to calculate
  32.  
  33. ### START CODE HERE ###
  34. init = tf.global_variables_initializer()
  35. sess = tf.Session()
  36. session.run(init)
  37. result = session.run(Y)
  38.  
  39. ### END CODE HERE ###
  40.  
  41. # close the session
  42. sess.close()
  43.  
  44. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement