首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow + Matplotlib动画

Tensorflow + Matplotlib动画
EN

Stack Overflow用户
提问于 2016-07-22 18:01:44
回答 1查看 1.4K关注 0票数 1

我正在尝试使用tensorflow实现生活的游戏,并使用matplotlib.animation来描述动画。虽然图像可以显示,但由于某种原因,它不是动画。下面是我使用的代码:

参考:http://learningtensorflow.com/lesson8/

IDE: Pycharm社区版

我在FuncAnimation中传递了blit=False,因为它在使用blit=True的Mac上不起作用。

代码语言:javascript
复制
shape = (50, 50)
initial_board = tf.random_uniform(shape, minval=0, maxval=2, dtype=tf.int32)
board = tf.placeholder(tf.int32, shape=shape, name='board')

def update_board(X):
# Check out the details at: https://jakevdp.github.io/blog/2013/08/07/conways-game-of-life/
# Compute number of neighbours,
N = convolve2d(X, np.ones((3, 3)), mode='same', boundary='wrap') - X
# Apply rules of the game
X = (N == 3) | (X & (N == 2))
return X

board_update = tf.py_func(update_board, [board], [tf.int32])
fig = plt.figure()

if __name__ == '__main__':
with tf.Session() as sess:
    initial_board_values = sess.run(initial_board)
    X = sess.run(board_update, feed_dict={board: initial_board_values})[0]

    def game_of_life(*args):
        A = sess.run(board_update, feed_dict={board: X})[0]
        plot.set_array(A)
        return plot,

    ani = animation.FuncAnimation(fig, game_of_life, interval=100, blit=False)

    plot = plt.imshow(X, cmap='Greys', interpolation='nearest')
    plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-23 01:20:17

显示图像时,显示的是静态图像而不是动画。您需要删除此行,如本教程所述:

代码语言:javascript
复制
plot = plt.imshow(X, cmap='Greys', interpolation='nearest')

Hint: you will need to remove the plt.show() from the earlier code to make this run!

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38523596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档