Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. Lets start this programming exercise by computing the following equation: Y=WX+bY=WX+b , where WW and XX are random matrices and b is a random vector.
  2.  
  3. Exercise: Compute WX+bWX+b where W,XW,X , and bb 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.  
  13. def linear_function():
  14. """
  15. Implements a linear function:
  16. Initializes W to be a random tensor of shape (4,3)
  17. Initializes X to be a random tensor of shape (3,1)
  18. Initializes b to be a random tensor of shape (4,1)
  19. Returns:
  20. result -- runs the session for Y = WX + b
  21. """
  22.  
  23. np.random.seed(1)
  24.  
  25. ### START CODE HERE ### (4 lines of code)
  26. X = tf.constant(X, shape = np.random.randn(3,1), name = X)
  27. W = tf.constant(W, shape = np.random.randn(4,3), name = W)
  28. b = tf.constant(b, shape = np.random.randn(4,1), name = b)
  29. Y = tf.constant(tf.add(tf.matmul(W, X), b), name = Y)
  30. ### END CODE HERE ###
  31.  
  32. # Create the session using tf.Session() and run it with sess.run(...) on the variable you want to calculate
  33.  
  34. ### START CODE HERE ###
  35. init = tf.global_variables_initializer()
  36. sess = tf.Session()
  37. session.run(init)
  38. result = session.run(Y)
  39.  
  40. ### END CODE HERE ###
  41.  
  42. # close the session
  43. sess.close()
  44.  
  45. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement