jupyter ipython display_Tensorflow从jupyter / Ipython运行动画
我正在通過水上的水滴的tensorflow示例,代碼:
#Import libraries for simulation
import tensorflow as tf
import numpy as np
#Imports for visualization
import PIL.Image
from io import BytesIO
from IPython.display import clear_output, Image, display
#A function for displaying the state of the pond's surface as an image.
def DisplayArray(a, fmt='jpeg', rng=[0,1]):
"""Display an array as a picture."""
a = (a - rng[0])/float(rng[1] - rng[0])*255
a = np.uint8(np.clip(a, 0, 255))
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
clear_output(wait = True)
display(Image(data=f.getvalue()))
sess = tf.InteractiveSession()
def make_kernel(a):
"""Transform a 2D array into a convolution kernel"""
a = np.asarray(a)
a = a.reshape(list(a.shape) + [1,1])
return tf.constant(a, dtype=1)
def simple_conv(x, k):
"""A simplified 2D convolution operation"""
x = tf.expand_dims(tf.expand_dims(x, 0), -1)
y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='SAME')
return y[0, :, :, 0]
def laplace(x):
"""Compute the 2D laplacian of an array"""
laplace_k = make_kernel([[0.5, 1.0, 0.5],
[1.0, -6., 1.0],
[0.5, 1.0, 0.5]])
return simple_conv(x, laplace_k)
N = 500
# Initial Conditions -- some rain drops hit a pond
# Set everything to zero
u_init = np.zeros([N, N], dtype=np.float32)
ut_init = np.zeros([N, N], dtype=np.float32)
# Some rain drops hit a pond at random points
for n in range(40):
a,b = np.random.randint(0, N, 2)
u_init[a,b] = np.random.uniform()
DisplayArray(u_init, rng=[-0.1, 0.1])
# Parameters:
# eps -- time resolution
# damping -- wave damping
eps = tf.placeholder(tf.float32, shape=())
damping = tf.placeholder(tf.float32, shape=())
# Create variables for simulation state
U ?= tf.Variable(u_init)
Ut = tf.Variable(ut_init)
# Discretized PDE update rules
U_ = U + eps * Ut
Ut_ = Ut + eps * (laplace(U) - damping * Ut)
# Operation to update the state
step = tf.group(
U.assign(U_),
Ut.assign(Ut_))
# Initialize state to initial conditions
tf.global_variables_initializer().run()
# Run 1000 steps of PDE
for i in range(1000):
# Step simulation
step.run({eps: 0.03, damping: 0.04})
DisplayArray(U.eval(), rng=[-0.1, 0.1])
然后從Ipython I import partial_d但它不會(huì)生成動(dòng)畫。
任何使用過tensorflow的人都知道如何解決這個(gè)問題? 谷歌提到Ipython Notebook,無法找到/設(shè)置,但我確實(shí)安裝了jupyter和最新的Ipython。
你以前用過jupyter嗎? 我認(rèn)為你需要啟動(dòng)筆記本服務(wù)器并從那里運(yùn)行代碼。
嘗試運(yùn)行jupyter notebook,然后將代碼導(dǎo)入筆記本。 或者,您可以將代碼復(fù)制并粘貼到代碼單元格中,然后跳過導(dǎo)入。
我不熟悉你所指的例子,但我不認(rèn)為這是一個(gè)TF問題。 了解如何通過jupyter運(yùn)行它(iPython的新名稱可以清除任何混淆)。
我熟悉python,剛剛安裝了jupyter,我確實(shí)運(yùn)行了代碼,但無法弄清楚如何從那里運(yùn)行腳本。以下是此drive.google.com/file/d/0B0nxIjitvEABMkQzZDktcUNyV3c/&hellip的兩個(gè)快照; drive.google.com/file/d/0B0nxIjitvEABUmppc3JyREg1ZDg/…
點(diǎn)擊新的新筆記本。確保它正確連接到python內(nèi)核(它應(yīng)該是)然后+一個(gè)新的代碼單元,復(fù)制你的代碼并運(yùn)行。我是否可以強(qiáng)烈建議您花幾分鐘時(shí)間研究一下jupyter筆記本。在編寫python時(shí),它們非常有用,特別是對(duì)于Tensorflow這樣的東西。我一直用它來進(jìn)行TF開發(fā)。不要打開.py文件,因?yàn)槟銦o法在jupyter中運(yùn)行它。您可以使用之前提到的命令導(dǎo)入,但我只是將它放在一個(gè)新筆記本的單元格中。
現(xiàn)在我想出了如何使用它是啊只是將它復(fù)制到單元格然后運(yùn)行它工作,你如何導(dǎo)入代碼?
我相信有一個(gè)導(dǎo)入命令,但我從來沒有使用它。我通常從頭開始,但如果沒有太多,你可以將代碼復(fù)制到單元格中。我將研究如何使用jupyter來優(yōu)化您的工作流程并在單元格之間拆分代碼。這對(duì)Tensorflow真有幫助!有關(guān)使用魔術(shù)命令導(dǎo)入代碼的更多信息,請(qǐng)參閱此內(nèi)容對(duì)于更進(jìn)一步的jupyter問題,請(qǐng)隨時(shí)創(chuàng)建一個(gè)新問題,將其鏈接到此處,我可以提供幫助,因?yàn)榇藂已得到解答
這讓我快速了解如何使用jupyter和tensorflow來生成漣漪動(dòng)畫。
總結(jié)
以上是生活随笔為你收集整理的jupyter ipython display_Tensorflow从jupyter / Ipython运行动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 释放指针_C库释放来自Rust
- 下一篇: java web定义数组_Java基础