Problems encountered by Python using keras and tensorflow

Script file named keras error report

Keras has been installed by pip, but it still cannot be used. I tried to add functions and reinstalled keras but failed. Finally I saw an article about the script naming problem. After trying, the problem was solved. The specific error record is as follows:

No module named 'keras.datasets'; 'keras' is not a package

Solution:

(1). Modify the naming in the project script. The script cannot be called keras, otherwise an error will be reported.
(2). Re-download the keras package

pip install keras tensorflow==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/ --default-timeout=100

Cannot find the corresponding package module during use

Use pip list to find that the corresponding module and installation package are there, but the module cannot be called. After thinking about what functions the package lacks or what dependencies are missing, I found on the Internet that the version of the package is too high and compatibility problems are caused. The package was upgraded to solve the problem that the package module could not be found.

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

Solution:

Uninstall the package, find the package of the matching version of keras and tensorflow, here I am upgrading the keras package

tensorflow-gpu installation

I have used the CPU's tensorflow before, this time the purpose is to use the GPU version, and to perform a new display of tensorflow visualization, and complete my previous verification of tensorflow's BP and convolutional neural network.

I installed the gpu version of tensorflow. I uninstalled the tensorflow of the cpu first. The reason is that if you don’t delete it, the python program will use the tensorflow of the cpu by default. I tried for 30 minutes during the installation process. The final installation solution is as follows:
use installation The command is:
pip install tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple/ --default-timeout=100

D:\Python\python_data2_project>pip install tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple/ --default-timeout=100

Case running of tensorflow and keras

The following code is taken from the network and successfully runs as follows:

import numpy as np
import os
import tensorflow
from keras.models import Sequential
from keras.layers import Dense
# 随机生成一组数据
data = np.random.random((1000,100))
# 随机生成标签
labels = np.random.randint(2,size=(1000,1))
model = Sequential()
# 添加一层神经网络
model.add(Dense(32,
          activation='relu',
          input_dim=100))
          # 添加激活函数(activate function)
model.add(Dense(1, activation='sigmoid'))
# 构建模型,定义优化器及损失函数
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])
              # 模型与数据一键fit
model.fit(data,labels,epochs=10,batch_size=32)
predictions = model.predict(data)

Insert picture description here
Through the final result of the above loss function, it can be seen that the effect of the model is not very good.

Tensorflow GPU setup operation

The computer graphics card type is not supported, and it has not been verified for the time being. Follow-up verification will continue
https://blog.csdn.net/A632189007/article/details/77978058

Plot the loss function

hist=model.fit(data,labels,epochs=10,batch_size=32)
plt.plot(hist.history['accuracy'],label="accuracy")
plt.plot(hist.history['loss'],label="loss")
plt.legend(loc=0, ncol=1)   # 参数:loc设置显示的位置,0是自适应;ncol设置显示的列数
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/tandelin/article/details/103512154