无法导入名称“ ops” python

时间:2018-06-28 06:42:49

标签: tensorflow machine-learning keras

我正在尝试运行应用程序。但是我遇到一个错误:

from createDB import load_dataset
import numpy as np
import keras
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
#################################################33
#show dataset
X_train,y_train,X_test,y_test = load_dataset()
print('Training data shape : ', X_train.shape, y_train.shape)
print('Testing data shape : ', X_test.shape, y_test.shape)
############################################################
# Find the unique numbers from the train labels
classes = np.unique(y_train)
nClasses = len(classes)
print('Total number of outputs : ', nClasses)
print('Output classes : ', classes)
###################################################
#plt.figure(figsize=[5,5])
#
## Display the first image in training data
#plt.subplot(121)
#plt.imshow(X_train[0,:,:], cmap='gray')
#plt.title("Ground Truth : {}".format(y_train[0]))
#
## Display the first image in testing data
#plt.subplot(122)
########################################################
#X_train.max()
#X_train.shape()
##################################
# normalization and float32
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train = X_train / 255.
X_test = X_test / 255.
###############################3
#Change the labels from categorical to one-hot encoding
y_train_one_hot = to_categorical(y_train)
y_test_one_hot = to_categorical(y_test)

# Display the change for category label using one-hot encoding
print('Original label:', y_train[25])
print('After conversion to one-hot:', y_train_one_hot[25])
############################################
# training split to trainig and validation
X_train,X_valid,train_label,valid_label = train_test_split(X_train, y_train_one_hot, test_size=0.2, random_state=13)
X_train.shape,
X_valid.shape,
train_label.shape,
valid_label.shape
#########################
batch_size = 64
epochs = 20
num_classes = 3
####################
fashion_model = Sequential()
fashion_model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',input_shape=(28,28,3),padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D((2, 2),padding='same'))
fashion_model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))
fashion_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
fashion_model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
fashion_model.add(LeakyReLU(alpha=0.1))                  
fashion_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
fashion_model.add(Flatten())
fashion_model.add(Dense(128, activation='linear'))
fashion_model.add(LeakyReLU(alpha=0.1))                  
fashion_model.add(Dense(num_classes, activation='softmax'))
  

文件“ F:\ anaconda \ install \ envs \ anaconda35 \ lib \ site-packages \ keras \ backend \ tensorflow_backend.py”,第6行,在       从tensorflow.python.framework导入ops为tf_ops

     

ImportError:无法导入名称“ ops”

如何解决此错误?

4 个答案:

答案 0 :(得分:2)

您可以尝试以下方法:

pip install tensorflow --upgrade
pip install keras --upgrade

也许Keras框架检查您的TensorFlow的后端版本太旧了。

答案 1 :(得分:1)

存在相同的问题,升级无法解决问题

我解决了做

sudo pip uninstall keras
sudo pip uninstall tensorflow

sudo pip install tensorflow
sudo pip install keras

现在工作正常。

答案 2 :(得分:0)

尝试先卸载:

pip uninstall tensorflow tensorflow-gpu protocol --yes

pip install tensorflow-gpu==1.9.0

pip install keras==2.2.0

答案 3 :(得分:-2)

通过pip卸载来删除keras,然后使用

conda install keras

如果您有conda发行版

相关问题