<jupyter_start><jupyter_text>build network<jupyter_code>def build_mlp(input_placeholder, output_size, scope, n_layers, size, activation=tf.tanh, output_activation=None):
    """
        Builds a feedforward neural network
        
        arguments:
            input_placeholder: placeholder variable for the state (batch_size, input_size)
            output_size: size of the output layer
            scope: variable scope of the network
            n_layers: number of hidden layers
            size: dimension of the hidden layer
            activation: activation of the hidden layers
            output_activation: activation of the ouput layers

        returns:
            output placeholder of the network (the result of a forward pass) 

        Hint: use tf.layers.dense    
    """
    # YOUR CODE HERE
    fwd = input_placeholder
    for _ in range(n_layers):
        fwd = tf.layers.dense(fwd, size, activation=activation)
    output_placeholder = tf.layers.dense(fwd, output_size, activation=output_activation)
    return output_placeholder
sess = tf_reset()
input_ph = tf.placeholder(dtype=tf.float32, shape = [None, 2])
output_ph = build_mlp(input_ph, 3, None, 2, 5, tf.nn.relu, tf.nn.softmax)

sess.run(tf.global_variables_initializer())
output_run = sess.run(output_ph, feed_dict={input_ph: np.array([[2,3], [4,5]])})
print(output_run)<jupyter_output>[[0.17747754 0.5960405  0.22648197]
 [0.07801642 0.7966605  0.12532304]]<jupyter_text>matrix broadcasting<jupyter_code>sess = tf_reset()
b = tf.constant([[1,1,1], [1,1,1]], dtype = tf.float32)
a = tf.constant([[2],[3]], dtype = tf.float32)
c = b * a
print(sess.run(c))<jupyter_output>[[2. 2. 2.]
 [3. 3. 3.]]<jupyter_text>random sampling<jupyter_code>sess = tf_reset()
# logits = tf.constant([[0,0,10],[10,0,0]], dtype = tf.float32)
logits = tf.constant([[0,0,10]], dtype = tf.float32)
samples = tf.random.categorical(logits, 1)
# samples = tf.reshape(samples, [1,-1])
print(sess.run(samples))
import tensorflow_probability as tfp
labels = tf.constant([0, 0], dtype = tf.int32)
dist = tfp.distributions.Categorical(logits)
print(sess.run(dist.log_prob(labels)))
print(sess.run(dist.prob(labels)))
1.0 / (np.exp(10)+2)
sess = tf_reset()
mu = tf.constant([[0, 0, 0], [1 ,2 ,3 ]], dtype = tf.float32)
std = tf.constant([.1, .1, 100], dtype = tf.float32)
samples = tf.random.normal(tf.shape(mu))
samples = mu + std * samples
print(sess.run(samples))
import tensorflow_probability as tfp
dist = tfp.distributions.MultivariateNormalDiag(loc = mu, scale_diag = std)
log_prob = dist.log_prob(mu)
print(sess.run(log_prob))
mult = log_prob * tf.constant([1,2], dtype = tf.float32)
print(sess.run(mult))
mult1 = tf.multiply(log_prob, tf.constant([1,2], dtype = tf.float32))
print(sess.run(mult1))
sess = tf_reset()
sy_logstd = tf.get_variable(name='log_std', shape=[5],
                initializer=tf.constant_initializer(1.0))
sess.run(tf.global_variables_initializer())
print(sess.run(sy_logstd))
dist = tfp.distributions.Normal(0, 1)
print(sess.run(dist.log_prob(0))*3)<jupyter_output>-2.756815552711487<jupyter_text>reshape<jupyter_code>xx = tf.reshape(tf.constant([[3]], dtype=tf.float32), [-1])
print(sess.run(xx))<jupyter_output>[3.]<jupyter_text>numpy<jupyter_code>from itertools import accumulate
import operator
ar = np.array([2,3,4, 5])
list(accumulate(ar[::-1], lambda bal, x: bal * 0.5 + x))[::-1]
np.array(list(accumulate(np.ones(10), lambda res, _: res * 0.5)))
from functools import reduce
reduce(lambda bal, x: bal * 0.5 + x, ar[::-1])<jupyter_output><empty_output><jupyter_text>Open AI gym<jupyter_code>import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
    env.render()
    env.step(env.action_space.sample()) # take a random action
env.close()
import gym
env = gym.make('CartPole-v0')
print(env.action_space.sample())
print(env.reset())
env.step(1)[0]
env.step(1)
env = gym.make('LunarLanderContinuous-v2')
env.action_space.sample()
env.action_space.contains(np.array([ 1.3631266 , -0.2566176]))
env.action_space.contains(np.array([0.9, -0.9]))
env.action_space<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup