简体   繁体   English

我在使用tensorflow的python中执行卷积神经网络程序期间遇到错误,错误是

[英]i am getting error during exection of convolution neural network program in python using tensorflow and the error is

I have installed all packages using pip in python 3.6.0. 我已经在python 3.6.0中使用pip安装了所有软件包。 Iam getting error during execution of my convolution neural network code in python which includes tensorflow like modules 我在python中执行卷积神经网络代码期间遇到错误,其中包括类似tensorflow的模块

this is the error 这是错误

Traceback (most recent call last):
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\imp.py", line 296, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    import _pywrap_tensorflow_internal
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\python folder\9781786464392_Code\Artificial_Intelligence_with_Python_Code\Chapter 16\code\cnn.py", line 3, in <module>
    import tensorflow as tf
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\imp.py", line 296, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\patlo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    import _pywrap_tensorflow_internal
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
>>> 

this is the actual code in python 这是python中的实际代码

import argparse

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

def build_arg_parser():
    parser = argparse.ArgumentParser(description='Build a CNN classifier \
            using MNIST data')
    parser.add_argument('--input-dir', dest='input_dir', type=str, 
            default='./mnist_data', help='Directory for storing data')
    return parser

def get_weights(shape):
    data = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(data)

def get_biases(shape):
    data = tf.constant(0.1, shape=shape)
    return tf.Variable(data)

def create_layer(shape):
    # Get the weights and biases 
    W = get_weights(shape)
    b = get_biases([shape[-1]])

    return W, b

def convolution_2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], 
            padding='SAME')

def max_pooling(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 
            strides=[1, 2, 2, 1], padding='SAME')

if __name__ == '__main__':
    args = build_arg_parser().parse_args()

    # Get the MNIST data
    mnist = input_data.read_data_sets(args.input_dir, one_hot=True)

    # The images are 28x28, so create the input layer 
    # with 784 neurons (28x28=784) 
    x = tf.placeholder(tf.float32, [None, 784])

    # Reshape 'x' into a 4D tensor 
    x_image = tf.reshape(x, [-1, 28, 28, 1])

    # Define the first convolutional layer
    W_conv1, b_conv1 = create_layer([5, 5, 1, 32])

    # Convolve the image with weight tensor, add the 
    # bias, and then apply the ReLU function
    h_conv1 = tf.nn.relu(convolution_2d(x_image, W_conv1) + b_conv1)

    # Apply the max pooling operator
    h_pool1 = max_pooling(h_conv1)

    # Define the second convolutional layer
    W_conv2, b_conv2 = create_layer([5, 5, 32, 64])

    # Convolve the output of previous layer with the 
    # weight tensor, add the bias, and then apply 
    # the ReLU function
    h_conv2 = tf.nn.relu(convolution_2d(h_pool1, W_conv2) + b_conv2)

    # Apply the max pooling operator
    h_pool2 = max_pooling(h_conv2)

    # Define the fully connected layer
    W_fc1, b_fc1 = create_layer([7 * 7 * 64, 1024])

    # Reshape the output of the previous layer
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])

    # Multiply the output of previous layer by the 
    # weight tensor, add the bias, and then apply 
    # the ReLU function
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    # Define the dropout layer using a probability placeholder
    # for all the neurons
    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    # Define the readout layer (output layer)
    W_fc2, b_fc2 = create_layer([1024, 10])
    y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

    # Define the entropy loss and the optimizer
    y_loss = tf.placeholder(tf.float32, [None, 10])
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_loss))
    optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)

    # Define the accuracy computation
    predicted = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_loss, 1))
    accuracy = tf.reduce_mean(tf.cast(predicted, tf.float32))

    # Create and run a session
    sess = tf.InteractiveSession()
    init = tf.initialize_all_variables()
    sess.run(init)

    # Start training
    num_iterations = 21000
    batch_size = 75
    print('\nTraining the model....')
    for i in range(num_iterations):
        # Get the next batch of images
        batch = mnist.train.next_batch(batch_size)

        # Print progress
        if i % 50 == 0:
            cur_accuracy = accuracy.eval(feed_dict = {
                    x: batch[0], y_loss: batch[1], keep_prob: 1.0})
            print('Iteration', i, ', Accuracy =', cur_accuracy)

        # Train on the current batch
        optimizer.run(feed_dict = {x: batch[0], y_loss: batch[1], keep_prob: 0.5})

    # Compute accuracy using test data
    print('Test accuracy =', accuracy.eval(feed_dict = {
            x: mnist.test.images, y_loss: mnist.test.labels, 
            keep_prob: 1.0}))

It could be because the following problems. 可能是因为以下问题。

  • Your CPU does not support AVX instructions which are needed by TensorFlow. 您的CPU不支持TensorFlow所需的AVX指令。

  • Not installed Microsoft C++ Redist 2015. 未安装Microsoft C ++ Redist 2015。

Solutions : 解决方案:

  • Use Anaconda. 使用水蟒。 Install it from their website and use it has your package manager. 从他们的网站上安装它,并使用它的软件包管理器。 It uses conda which is like pip. 它使用像pip一样的conda。 Create an enviroment in it and install TensorFlow using 在其中创建环境并使用安装TensorFlow

    conda install tensorflow

  • Install Microsoft C++ Redist 2015. 安装Microsoft C ++ Redist 2015。

  • Check if your CPU has AVX compatibility. 检查您的CPU是否具有AVX兼容性。 If yes, then try uninstalling and then reinstalling tensorflow. 如果是,请尝试卸载然后重新安装tensorflow。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 python 中使用 tensorflow 实现卷积神经网络程序时,我不断收到奇怪的“无效语法”错误 - I keep receiving a weird "invalid syntax" error when trying to implement a convolution neural network program using tensorflow in python AttributeError: 'dict' object has no attribute 'train' 尝试在 python 中使用 tensorflow 实现卷积神经网络程序时出现错误 - AttributeError: 'dict' object has no attribute 'train' error when trying to implement a convolution neural network program using tensorflow in python 在Python错误中将卷积神经网络与千层面结合使用 - Using Convolution Neural Net with Lasagne in Python error 神经网络 Python 错误“无法获得卷积算法” - Neural Network Python Error "Failed to get convolution algorithm" 使用keras和tensorflow的卷积神经网络(CNN)的输入应该是什么? - What should be the input to Convolution neural network (CNN) using keras and tensorflow? Tensorflow 卷积神经网络负维大小 - Tensorflow Convolution Neural Network Negative Dimension size 在对神经网络进行编程时python中的错误(相当容易……我是python的新手) - Error in python while programming neural Network (fairly easy… I am new with python) 具有不同尺寸图像的张量流卷积神经网络 - Tensorflow Convolution Neural Network with different sized images 使用 tensorflow 进行图像分割的卷积神经网络 - Convolution neural network for image segmentation with tensorflow 为什么我在这个神经网络中接收 2 个值的 2 值元组上出现值错误? - Why am I getting a value error on a 2 Valued tuple receiving 2 values in this neural network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM