Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- 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):
- X = tf.constant(np.random.randn(3,1), name = "X")
- You might find the following functions helpful:
- tf.matmul(..., ...) to do a matrix multiplication
- tf.add(..., ...) to do an addition
- np.random.randn(...) to initialize randomly
- def linear_function():
- """
- Implements a linear function:
- Initializes W to be a random tensor of shape (4,3)
- Initializes X to be a random tensor of shape (3,1)
- Initializes b to be a random tensor of shape (4,1)
- Returns:
- result -- runs the session for Y = WX + b
- """
- np.random.seed(1)
- ### START CODE HERE ### (4 lines of code)
- X = tf.constant(X, shape = np.random.randn(3,1), name = X)
- W = tf.constant(W, shape = np.random.randn(4,3), name = W)
- b = tf.constant(b, shape = np.random.randn(4,1), name = b)
- Y = tf.constant(tf.add(tf.matmul(W, X), b), name = Y)
- ### END CODE HERE ###
- # Create the session using tf.Session() and run it with sess.run(...) on the variable you want to calculate
- ### START CODE HERE ###
- init = tf.global_variables_initializer()
- sess = tf.Session()
- session.run(init)
- result = session.run(Y)
- ### END CODE HERE ###
- # close the session
- sess.close()
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement