2. Using the nested for loops(i.e. for loop defined inside a for loop) to demons
ID: 3864333 • Letter: 2
Question
2. Using the nested for loops(i.e. for loop defined inside a for loop) to demonstrate the law of large numbers with R. (a) Take sample from X Uniform (3,7. (b) Run your outer for loop from 10 to 2010 with step size 100 so that the sample size gets increased: 2010 (c) Run the inner for loop to construct the sampling distribution of the mean with 100000 random numbers. (d) Compute mean of the sampling distribution and standard deviations for each n in the outer loop and print them out one row, (e.g. print(c(mean,std)). (e) Generate the histogram for each sample size n with fixed xlim c(3.5,6.5) n the outer loop. You may use par (mfrow c (3,3) before the loops begin to see the slide.Explanation / Answer
import tensorflow as tf def inner_loop(t): def fn(n): return n + var # using var here leads to the error. return tf.map_fn(fn=fn, elems=t, parallel_iterations=1) def outer_loop(input): def fn(n): return inner_loop(n) #if I would return inner_loop(n)+var here no errors appear. return tf.map_fn(fn=fn, elems=input, parallel_iterations=1) with tf.Session() as sess: var = tf.Variable(tf.constant(1.0)) input = tf.to_float(tf.convert_to_tensor([[1,2],[3,4],[5,6]])) res = outer_loop(input) optimizer = tf.train.AdamOptimizer(learning_rate=0.001) trainOperation = optimizer.minimize(tf.reduce_mean(tf.square(res))) sess.run(tf.initialize_all_variables()) sess.run(trainOperation)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.