与tensorflow 1.0 mnist代码错误(Error with tensorflow 1.0 mnist code)
我现在正在用python 3.5.2学习tensorflow 1.0。 我尝试了在github上找到的下面的代码,但是我得到错误没有名为'tensorflowvisu'的模块。 如果我删除导入tensorflowvisu我得到错误我= tensorflowvisu.tf_format_mnist_images(X,Ypred,Y_)#默认组装10x10图像NameError:名称'tensorflowvisu'未定义我该怎么做才能让此代码工作? 有没有人有与tensorflow 1.0和python 3.5,我可以按照学习mnist工作代码? 任何回应赞赏。 https://github.com/martin-gorner/tensorflow-mnist-tutorial/blob/master/mnist_1.0_softmax.py
import tensorflow as tf import tensorflowvisu from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets tf.set_random_seed(0) # neural network with 1 layer of 10 softmax neurons # # · · · · · · · · · · (input data, flattened pixels) X [batch, 784] # 784 = 28 * 28 # \x/x\x/x\x/x\x/x\x/ -- fully connected layer (softmax) W [784, 10] b[10] # · · · · · · · · Y [batch, 10] # The model is: # # Y = softmax( X * W + b) # X: matrix for 100 grayscale images of 28x28 pixels, flattened (there are 100 images in a mini-batch) # W: weight matrix with 784 lines and 10 columns # b: bias vector with 10 dimensions # +: add with broadcasting: adds the vector to each line of the matrix (numpy) # softmax(matrix) applies softmax on each line # softmax(line) applies an exp to each value then divides by the norm of the resulting line # Y: output matrix with 100 lines and 10 columns # Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels) mnist = read_data_sets("data", one_hot=True, reshape=False, validation_size=0) # input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch X = tf.placeholder(tf.float32, [None, 28, 28, 1]) # correct answers will go here Y_ = tf.placeholder(tf.float32, [None, 10]) # weights W[784, 10] 784=28*28 W = tf.Variable(tf.zeros([784, 10])) # biases b[10] b = tf.Variable(tf.zeros([10])) # flatten the images into a single line of pixels # -1 in the shape definition means "the only possible dimension that will preserve the number of elements" XX = tf.reshape(X, [-1, 784]) # The model Y = tf.nn.softmax(tf.matmul(XX, W) + b) # loss function: cross-entropy = - sum( Y_i * log(Yi) ) # Y: the computed output vector # Y_: the desired output vector # cross-entropy # log takes the log of each element, * multiplies the tensors element by element # reduce_mean will add all the components in the tensor # so here we end up with the total cross-entropy for all images in the batch cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0 # normalized for batches of 100 images, # *10 because "mean" included an unwanted division by 10 # accuracy of the trained model, between 0 (worst) and 1 (best) correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # training, learning rate = 0.005 train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy) # matplotlib visualisation allweights = tf.reshape(W, [-1]) allbiases = tf.reshape(b, [-1]) I = tensorflowvisu.tf_format_mnist_images(X, Y, Y_) # assembles 10x10 images by default It = tensorflowvisu.tf_format_mnist_images(X, Y, Y_, 1000, lines=25) # 1000 images on 25 lines datavis = tensorflowvisu.MnistDataVis() # init init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # You can call this function in a loop to train the model, 100 images at a time def training_step(i, update_test_data, update_train_data): # training on batches of 100 images with 100 labels batch_X, batch_Y = mnist.train.next_batch(100) # compute training values for visualisation if update_train_data: a, c, im, w, b = sess.run([accuracy, cross_entropy, I, allweights, allbiases], feed_dict={X: batch_X, Y_: batch_Y}) datavis.append_training_curves_data(i, a, c) datavis.append_data_histograms(i, w, b) datavis.update_image1(im) print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c)) # compute test values for visualisation if update_test_data: a, c, im = sess.run([accuracy, cross_entropy, It], feed_dict={X: mnist.test.images, Y_: mnist.test.labels}) datavis.append_test_curves_data(i, a, c) datavis.update_image2(im) print(str(i) + ": ********* epoch " + str(i*100//mnist.train.images.shape[0]+1) + " ********* test accuracy:" + str(a) + " test loss: " + str(c)) # the backpropagation training step sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y}) datavis.animate(training_step, iterations=2000+1, train_data_update_freq=10, test_data_update_freq=50, more_tests_at_start=True) # to save the animation as a movie, add save_movie=True as an argument to datavis.animate # to disable the visualisation use the following line instead of the datavis.animate line # for i in range(2000+1): training_step(i, i % 50 == 0, i % 10 == 0) print("max test accuracy: " + str(datavis.get_max_test_accuracy())) # final max test accuracy = 0.9268 (10K iterations). Accuracy should peak above 0.92 in the first 2000 iterations.
I am now learning tensorflow 1.0 with python 3.5.2. I tried the following code found on github but i am getting the error No module named 'tensorflowvisu'. If i remove the import tensorflowvisu i get the error I = tensorflowvisu.tf_format_mnist_images(X, Ypred, Y_) # assembles 10x10 images by default NameError: name 'tensorflowvisu' is not defined What should i do to get this code to work? Does anyone have a working code for mnist with tensorflow 1.0 and python 3.5 that i can follow to learn? Any response appreciated. https://github.com/martin-gorner/tensorflow-mnist-tutorial/blob/master/mnist_1.0_softmax.py
import tensorflow as tf import tensorflowvisu from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets tf.set_random_seed(0) # neural network with 1 layer of 10 softmax neurons # # · · · · · · · · · · (input data, flattened pixels) X [batch, 784] # 784 = 28 * 28 # \x/x\x/x\x/x\x/x\x/ -- fully connected layer (softmax) W [784, 10] b[10] # · · · · · · · · Y [batch, 10] # The model is: # # Y = softmax( X * W + b) # X: matrix for 100 grayscale images of 28x28 pixels, flattened (there are 100 images in a mini-batch) # W: weight matrix with 784 lines and 10 columns # b: bias vector with 10 dimensions # +: add with broadcasting: adds the vector to each line of the matrix (numpy) # softmax(matrix) applies softmax on each line # softmax(line) applies an exp to each value then divides by the norm of the resulting line # Y: output matrix with 100 lines and 10 columns # Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels) mnist = read_data_sets("data", one_hot=True, reshape=False, validation_size=0) # input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch X = tf.placeholder(tf.float32, [None, 28, 28, 1]) # correct answers will go here Y_ = tf.placeholder(tf.float32, [None, 10]) # weights W[784, 10] 784=28*28 W = tf.Variable(tf.zeros([784, 10])) # biases b[10] b = tf.Variable(tf.zeros([10])) # flatten the images into a single line of pixels # -1 in the shape definition means "the only possible dimension that will preserve the number of elements" XX = tf.reshape(X, [-1, 784]) # The model Y = tf.nn.softmax(tf.matmul(XX, W) + b) # loss function: cross-entropy = - sum( Y_i * log(Yi) ) # Y: the computed output vector # Y_: the desired output vector # cross-entropy # log takes the log of each element, * multiplies the tensors element by element # reduce_mean will add all the components in the tensor # so here we end up with the total cross-entropy for all images in the batch cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0 # normalized for batches of 100 images, # *10 because "mean" included an unwanted division by 10 # accuracy of the trained model, between 0 (worst) and 1 (best) correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # training, learning rate = 0.005 train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy) # matplotlib visualisation allweights = tf.reshape(W, [-1]) allbiases = tf.reshape(b, [-1]) I = tensorflowvisu.tf_format_mnist_images(X, Y, Y_) # assembles 10x10 images by default It = tensorflowvisu.tf_format_mnist_images(X, Y, Y_, 1000, lines=25) # 1000 images on 25 lines datavis = tensorflowvisu.MnistDataVis() # init init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # You can call this function in a loop to train the model, 100 images at a time def training_step(i, update_test_data, update_train_data): # training on batches of 100 images with 100 labels batch_X, batch_Y = mnist.train.next_batch(100) # compute training values for visualisation if update_train_data: a, c, im, w, b = sess.run([accuracy, cross_entropy, I, allweights, allbiases], feed_dict={X: batch_X, Y_: batch_Y}) datavis.append_training_curves_data(i, a, c) datavis.append_data_histograms(i, w, b) datavis.update_image1(im) print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c)) # compute test values for visualisation if update_test_data: a, c, im = sess.run([accuracy, cross_entropy, It], feed_dict={X: mnist.test.images, Y_: mnist.test.labels}) datavis.append_test_curves_data(i, a, c) datavis.update_image2(im) print(str(i) + ": ********* epoch " + str(i*100//mnist.train.images.shape[0]+1) + " ********* test accuracy:" + str(a) + " test loss: " + str(c)) # the backpropagation training step sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y}) datavis.animate(training_step, iterations=2000+1, train_data_update_freq=10, test_data_update_freq=50, more_tests_at_start=True) # to save the animation as a movie, add save_movie=True as an argument to datavis.animate # to disable the visualisation use the following line instead of the datavis.animate line # for i in range(2000+1): training_step(i, i % 50 == 0, i % 10 == 0) print("max test accuracy: " + str(datavis.get_max_test_accuracy())) # final max test accuracy = 0.9268 (10K iterations). Accuracy should peak above 0.92 in the first 2000 iterations.
原文:https://stackoverflow.com/questions/42707081
最满意答案
我现在没有打开团结,但我认为
GetComponent<ParticleSystem>().Stop();
是你需要的。 您可以使用重启系统
GetComponent<ParticleSystem>().Play();
此外,如果经常这样做,您应该考虑将粒子系统保存在类变量中。
I don't have unity opened right now, but I think that
GetComponent<ParticleSystem>().Stop();
is what you need. You can restart the system using
GetComponent<ParticleSystem>().Play();
Also, if you do this often, you should consider keeping your particle system in a class variable.
相关问答
更多-
您应该关闭粒子系统中的循环,或者在脚本中的Awake()上停止()或暂停()它,单击开火按钮时调用Play()。 查看此页面: http : //docs.unity3d.com/ScriptReference/ParticleSystem.html 您提供的样本不采用粒子系统,而是一些“抛射物”预制件,它基本上会在您单击鼠标按钮时生成预制实例。 这只是一个非常基本的射击机制的例子。 我相信在该脚本上命名可能会更好。 (我相信这个样本来自Unity的Merry Fragmas视频之一,他们以类似的方式使用某 ...
-
您可以创建自己的基本粒子系统。 该网站通过修改内置的粒子系统来解释创建新粒子系统的基础知识: https : //www.raywenderlich.com/128745/scene-kit-tutorial-swift-part-5-particle-systems You may be able to create your own basic particle systems. This site explains the basics of creating a new particle syste ...
-
我现在没有打开团结,但我认为 GetComponent
().Stop(); 是你需要的。 您可以使用重启系统 GetComponent ().Play(); 此外,如果经常这样做,您应该考虑将粒子系统保存在类变量中。 I don't have unity opened right now, but I think that GetComponent ().Stop(); is what you need. Yo ... -
Unity如果父项被禁用,如何播放粒子系统效果?(Unity How to play a particle system effect if when the parent get's disabled?)[2022-04-29]
创建名为ParticlesHolder的 GameObject。 将下面的脚本附加到它,然后确保将编辑器的大小更改为2 。 将两个粒子放到每个槽中。 我们的想法是不会disabled粒子。 public class ParticleHolder : MonoBehaviour { public ParticleSystem[] effects; public void playParticle(int particleNumber, Vector3 particlePos) { ... -
目前,您的LeafStormParticleSystem变量为您提供了ParticleSystem组件所在的LeafStormParticleSystem的引用,但不提供ParticleSystem本身。 因此,您无法访问粒子系统的成员,如maxParticles和emission 。 有两种方法可以解决这个问题: 当需要访问粒子系统时调用GetComponent
() 。 如果执行得太频繁,这可能代价很高 - 修改成员的语法是: LeafStormParticleSystem ... -
避免使用OnGui它已被弃用了。 Unity通过其新的GUI系统支持此功能。 但是,使用OnGUI是不可能的(不使用第三方工具)。 Avoid using OnGui it's pretty deprecated. Unity supports this feature with its new GUI system. However, using OnGUI it is not possible (without using 3rd party tools).
-
破坏粒子系统(Destroying particle systems)[2023-05-25]
Instantiate返回一个Object类型的对象,它是Unity中的顶级类(不是.NET类型)。 由于你使用var效果,编译器很好地使效果成为一个Object。 但是你需要一个GameObject,因为Object没有gameObject成员。 var effect = (GameObject)Instantiate(coinEffect, transform.position, transform.rotation); 这是使用var而不是强类型变量的危险之一。 最好的是: GameObject e ... -
您可以使用OnParticleCollision和ParticlePhysicsExtensions ,并使用PlayClipAtPoint播放声音: using UnityEngine; using System.Collections; [RequireComponent(typeof(ParticleSystem))] public class CollidingParticles : MonoBehaviour { public AudioClip collisionSFX; ...
-
您可以更改ParticleSystem组件的Simulation Space设置: Particle System主模块 。 编辑因为我一开始没有注意到你希望粒子的速度随着相机的加速而增加(我的不好......)这里你可以伪造效果: public class TestScript : MonoBehaviour { #region Attributes [SerializeField] private float m_MaxTransformSpeed; [Serializ ...
-
说明 这是因为Transform.position是Vector3类型的属性,它是一个结构。 C#中的结构是一种值类型。 这里发生了什么: 由于结构是值类型collisionObject.transform.position返回一些Vector3的副本 通过执行.x = xPos您正在尝试修改某些Vector3结构的此副本的x成员。 该副本将立即被丢弃。 因此,你的任务毫无用处。 这就是编译器给你一个错误的原因。 解 如果要为Transform.position指定新值,则需要按如下方式执行: Vector ...