2017-04-05 46 views
1

'' '導入文庫模擬' ''如何在Mandlebrot tensorflow program.Current輸出顯示圖像是<IPython.core.display.Image對象>

import tensorflow as tf 
import numpy as np 

'' '進口用於可視化'」 「

from PIL.Image 
from io import BytesIO 
from IPython.display import Image, display 

‘’」現在,我們將定義一個函數來實際顯示圖像,一旦我們有 迭代次數「」」

def DisplayFractal(a, fmt='jpeg'): 

    img =np.concatenate([10+20*np.cos(a_cyclic),30+50*np.sin(a_cyclic),155- 
    80*np.cos(a_cyclic)], 2) 
    img[a==a.max()] = 0 
    a = img 
    a = np.uint8(np.clip(a, 0, 255)) 
    f = BytesIO() 
    PIL.Image.fromarray(a).save(f, fmt) 
    display(Image(data=f.getvalue())) 


sess = tf.InteractiveSession() 
# Use NumPy to create a 2D array of complex numbers 

Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005] 
Z = X+1j*Y 
print(Z) 
#Now we define and initialize TensorFlow tensors. 

xs = tf.constant(Z.astype(np.complex64)) 
zs = tf.Variable(xs) 
ns = tf.Variable(tf.zeros_like(xs, tf.float32)) 


tf.global_variables_initializer().run() 

zs_ = zs*zs + xs 
print(zs) 

# Have we diverged with this new value? 
not_diverged = tf.abs(zs_) < 4 

''' 用於更新zs和迭代次數的操作。 注意:我們在計算zs後保持不同!這 是非常浪費!還有更好的,如果有點簡單的方法來做到這一點。 ''」 步驟= tf.group(zs.assign(zs_),ns.assign_add(tf.cast(not_diverged, tf.float32)))

for i in range(200): step.run() 

DisplayFractal(ns.eval()) 
+0

請添加關於您需要詢問的問題的適當描述 – Surajano

+0

@Surajano hello!它只是Mandlebrot tensorflow上的一個程序(鏈接https://www.tensorflow.org/tutorials/mandelbrot)會把你的..我只是想得到如何得到正確的輸出(作爲一個圖像)。 –

+0

@Surajano代碼與提到的代碼相同,並且它們沒有錯誤,但輸出不像描述的那樣 –

回答

1

我有同樣的問題。你必須運行Jupyter筆記本TensorFlow例如:如果你從其他IDE運行它像(Spyder的) http://jupyter.org/

你將看到的是在控制檯<IPython.core.display.Image object>

+0

謝謝!是的它現在可以工作! –

+0

這意味着:'pip install jupyter-console' &&執行'jupyter-console',然後在jupyter-console提示符下輸入[1]:'只需輸入:'run .py' –

0

但真正的問題是藿做纔不至於jupyter ....

-2

emmm,我毀了這個問題,你可以把我的功能看:

def displayFractal(a,fmt='jpeg'): 
    a_cyclic=(6.28*a/200.0).reshape(list(a.shape)+[1]) 

    # emmm I have changed the number. you can just continue your number   
    img=np.concatenate([5+10*np.cos(a_cyclic),15+25*np.sin(a_cyclic),70-40*np.cos(a_cyclic)],2) 

    img[a==a.max()]=0 
    a=img 
    a=np.uint8(np.clip(a,0,255)) 
    plt.imshow(PIL.Image.fromarray(a)) 
    plt.show() 
當然

你首先應該將matplotlib .pyplot導入爲plt。

相關問題