我是一名新的Python用户,也是“Stack Overflow”中的新用户,当我尝试编译张量流代码时,我遇到了一些问题,并且我无法从网站上找到答案,所以我想从这里获得一些帮助,谢谢大家提前!
\n\n这是我的编译结果:
\n\nD:\\Python\\Anaconda2\\envs\\tensorflow\\python.exe D:/Python/pycharm_project/test/mnist_chuji\nTraceback (most recent call last):\n File "D:/Python/pycharm_project/test/mnist_chuji", line 52, in <module>\n DisplayArray(u_init, rng=[-0.1, 0.1])\n File "D:/Python/pycharm_project/test/mnist_chuji", line 15, in DisplayArray\n PIL.Image.fromarray(a).save(f, fmt)\nNameError: name \'PIL\' is not defined\n\nProcess finished with exit code 1 \nRun Code Online (Sandbox Code Playgroud)\n\n这是我的代码,我标记了我的错误发生的行号,以便您轻松找到它:
\n\n#\xe5\xaf\xbc\xe5\x85\xa5\xe6\xa8\xa1\xe6\x8b\x9f\xe4\xbb\xbf\xe7\x9c\x9f\xe9\x9c\x80\xe8\xa6\x81\xe7\x9a\x84\xe5\xba\x93\nimport tensorflow as tf\nimport numpy as np\n\n#\xe5\xaf\xbc\xe5\x85\xa5\xe5\x8f\xaf\xe8\xa7\x86\xe5\x8c\x96\xe9\x9c\x80\xe8\xa6\x81\xe7\x9a\x84\xe5\xba\x93\nfrom PIL import Image\nfrom io import StringIO #python3 \xe4\xbd\xbf\xe7\x94\xa8\xe4\xba\x86io\xe4\xbb\xa3\xe6\x9b\xbf\xe4\xba\x86sStringIO\nfrom IPython.display import clear_output, Image, display\n\ndef DisplayArray(a, fmt=\'jpeg\', rng=[0,1]):\n """Display an array as a picture."""\n a = (a - rng[0])/float(rng[1] - rng[0])*255\n a = np.uint8(np.clip(a, 0, 255))\n f = StringIO()\n PIL.Image.fromarray(a).save(f, fmt) #line 15\n display(Image(data=f.getvalue()))\n\nsess = tf.InteractiveSession()\n\ndef make_kernel(a):\n """Transform a 2D array into a convolution kernel"""\n a = np.asarray(a)\n a = a.reshape(list(a.shape) + [1,1])\n return tf.constant(a, dtype=1)\n\ndef simple_conv(x, k):\n """A simplified 2D convolution operation"""\n x = tf.expand_dims(tf.expand_dims(x, 0), -1)\n y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding=\'SAME\')\n return y[0, :, :, 0]\n\ndef laplace(x):\n """Compute the 2D laplacian of an array"""\n laplace_k = make_kernel([[0.5, 1.0, 0.5],\n [1.0, -6., 1.0],\n [0.5, 1.0, 0.5]])\n return simple_conv(x, laplace_k)\n\nN = 500\n\n# Initial Conditions -- some rain drops hit a pond\n\n# Set everything to zero\nu_init = np.zeros([N, N], dtype="float32")\nut_init = np.zeros([N, N], dtype="float32")\n\n# Some rain drops hit a pond at random points\nfor n in range(40):\n a,b = np.random.randint(0, N, 2)\n u_init[a,b] = np.random.uniform()\n\nDisplayArray(u_init, rng=[-0.1, 0.1]) #line 52\n\n# Parameters:\n# eps -- time resolution\n# damping -- wave damping\neps = tf.placeholder(tf.float32, shape=())\ndamping = tf.placeholder(tf.float32, shape=())\n\n# Create variables for simulation state\nU = tf.Variable(u_init)\nUt = tf.Variable(ut_init)\n\n# Discretized PDE update rules\nU_ = U + eps * Ut\nUt_ = Ut + eps * (laplace(U) - damping * Ut)\n\n# Operation to update the state\nstep = tf.group(\n U.assign(U_),\n Ut.assign(Ut_))\n\n# Initialize state to initial conditions\ntf.initialize_all_variables().run()\n\n# Run 1000 steps of PDE\nfor i in range(1000):\n # Step simulation\n step.run({eps: 0.03, damping: 0.04})\n # Visualize every 50 steps\n if i % 50 == 0:\n clear_output()\n DisplayArray(U.eval(), rng=[-0.1, 0.1])\nRun Code Online (Sandbox Code Playgroud)\n\n我已经在我的张量流环境(python 3.5.2)中安装了枕头。
\n\n非常感谢大家!
\n| 归档时间: |
|
| 查看次数: |
15418 次 |
| 最近记录: |