0.jupyter notebook使用小技巧
(1)查看函数的参数和使用方法
第一种方法,在函数后面加上'?'。
如下,想要查看array函数的用法:
np.array?
----------
Docstring:
array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0,
like=None)
Create an array.
Parameters
可知,这是一个数组生成函数。
第二种方法,光标放置在函数上,然后按shift+Tab键,会弹出来函数用法界面。
Docstring:
array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0,
like=None)
1.步入正题,附上代码
import keras
from keras.layers import *
from keras.models import Model, Sequential
import pandas as pd
import numpy as np
from keras.utils.vis_utils import plot_model
from keras.utils import np_utils
import keras.backend as K
from sklearn.metrics import accuracy_score
import sklearn
import matplotlib.pyplot as plt
from IPython.display import Image
from tqdm import tqdm
from collections import deque
import seaborn as sns
import random,time
%matplotlib inline
import os#operating system,顾名思义,提供的是程序与操作系统进行交互的接口
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'#TF_CPP_MIN_LOG_LEVEL=3,只输出fatal(致命错误)
trainx_source = pd.read_csv('C:\\Users\\lenovo\\Desktop\\design.by.wyfxz\\hp.csv',header=None ) # 读取训练数据
trainx_source= np.array(trainx_source)
trainx_source=trainx_source.T
a=trainx_source[:,0:18]
b=trainx_source[:,-1]
a=np.expand_dims(a,axis=1)
a=np.expand_dims(a,axis=3)
X_train,y_train=a,b
X_test,y_test=a,b
num_actions = 3
dummy_actions = np.ones((1, num_actions))
image_w, image_h = 1,18
num_actions = 3
dummy_actions = np.ones((1, num_actions))
image_w, image_h = 1,18
class MnEnviroment(object):
def __init__(self, x,y):
self.train_X = x
self.train_Y = y
self.current_index = self._sample_index()
self.action_space = len(set(y)) - 1
def reset(self):
obs, _ = self.step(-1)
return obs
def step(self, action):
if action==-1:
_c_index = self.current_index
self.current_index = self._sample_index()
return (self.train_X[_c_index], 0)
r = self.reward(action)
self.current_index = self._sample_index()
return self.train_X[self.current_index], r
def reward(self, action):
c = self.train_Y[self.current_index]
#print(c)
return 1 if c==action else -1
def sample_actions(self):
return random.randint(0, self.action_space)
def _sample_index(self):
return random.randint(0, len(self.train_Y)-1)
env = MnEnviroment(X_train, y_train)
memory = deque(maxlen=512)
replay_size = 64
epoches = 200
pre_train_num = 256
gamma = 0. #every state is i.i.d
alpha = 0.5
forward = 512
epislon_total = 2018
def createDQN(input_width, input_height, actions_num):
img_input = Input(shape=(input_width, input_height,1),dtype='float32',name='image_inputs')
#conv1
conv1 = Conv2D(32,3,padding='same',activation='relu',kernel_initializer='he_normal')(img_input)
x = Flatten()(conv1)
x = Dense(128, activation='relu')(x)
outputs_q = Dense(actions_num, name='q_outputs')(x)
#one hot input
actions_input = Input((actions_num,),name='actions_input')
q_value= multiply([actions_input, outputs_q])
q_value = Lambda(lambda l:K.sum(l, axis=1,keepdims=True),name='q_value')(q_value)
model = Model(inputs=[img_input, actions_input], outputs=q_value)
model.compile(loss='mse',optimizer='adam')
return model
actor_model = createDQN(image_w,image_h,num_actions) #用于决策
critic_model = createDQN(image_w,image_h,num_actions) #用于训练
actor_q_model = Model(inputs=actor_model.input, outputs=actor_model.get_layer('q_outputs').output)
def copy_critic_to_actor():
critic_weights = critic_model.get_weights()
actor_wegiths = actor_model.get_weights()
for i in range(len(critic_weights)):
actor_wegiths[i] = critic_weights[i]
actor_model.set_weights(actor_wegiths)
def get_q_values(model_,state):
inputs_ = [state.reshape(1,*state.shape),dummy_actions]
qvalues = model_.predict(inputs_)
return qvalues[0]
def predict(model,states):
inputs_ = [states, np.ones(shape=(len(states),num_actions))]
qvalues = model.predict(inputs_)
return np.argmax(qvalues,axis=1)
def epsilon_calc(step, ep_min=0.01,ep_max=1,ep_decay=0.0001,esp_total = 1000):
return max(ep_min, ep_max -(ep_max - ep_min)*step/esp_total )
def epsilon_greedy(env, state, step, ep_min=0.01, ep_decay=0.0001,ep_total=1000):
epsilon = epsilon_calc(step, ep_min, 1, ep_decay,ep_total)
if np.random.rand()<epsilon:
return env.sample_actions(),0
qvalues = get_q_values(actor_q_model, state)
return np.argmax(qvalues), np.max(qvalues)
def pre_remember(pre_go = 30):
state = env.reset()
for i in range(pre_go):
rd_action = env.sample_actions()
next_state, reward = env.step(rd_action)
remember(state,rd_action,0,reward,next_state)
state = next_state
def remember(state,action,action_q,reward,next_state):
memory.append([state,action,action_q,reward,next_state])
def sample_ram(sample_num):
return np.array(random.sample(memory,sample_num))
存在问题
(1)No module named 'keras.utils.vis_utils'
在准备复现大三学长的代码时出现了这个问题。
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[39], line 6
4 import pandas as pd
5 import numpy as np
----> 6 from keras.utils.vis_utils import plot_model
7 from keras.utils import np_utils
8 import keras.backend as K
ModuleNotFoundError: No module named 'keras.utils.vis_utils'
解决方法:
from keras.utils.vis_utils import plot_model改为 from tensorflow.keras.utils import plot_model
原因:
由于keras版本的升级,在__init__.py里可以看到,包的迁移导致了在anaconda中就算安装了np_utils包,还是导入不进来。
(2)cannot import name 'np_utils' from 'keras.utils'
解决掉上一个问题之后,运行之后又出现了这个问题,现根据同样的方法进行修改:
改成了from tensorflow.keras.utils import np_utils,然后出现了cannot import name 'np_utils' from 'tensorflow.keras.utils'的问题,再改成这个样子就能完全解决:
from tensorflow.python.keras.utils.np_utils import to_categorical
这个问题的方法有些缺漏,虽然用这个方法不报错了,但是下文使用这个函数包时出现了新的问题,说找不到这个函数,所以最后改成了:
from tensorflow.keras.utils import to_categorical
(3)No module named 'sklearn'
解决了前两个问题之后,又出现了新的问题,查阅其他的博客之后,只需要最简单的pip install scikit-learn,这样就能完美解决啦
---------------------------------------------------------------------------------------------------------2023.8.17的11:20
二话不说,附代码
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'
trainx_source = pd.read_csv('hp.csv',header=None ) # 读取训练数据
trainx_source= np.array(trainx_source)
trainx_source=trainx_source.T
a=trainx_source[:,0:18]
b=trainx_source[:,-1]
a=np.expand_dims(a,axis=1)
a=np.expand_dims(a,axis=3)
X_train,y_train=a,b
X_test,y_test=a,b
运行以上代码出现了一个问题
具体报错是: No such file or directory: 'hp.csv',翻译之后是“没有这样的文件或目录”,所以我们就要找到表格所在的目录,找到文件所在的路径。
因为我放在了桌面里,所以我直接在桌面上打开了文件夹。
接着打开文件夹找到,文件所在位置
我们可以直接粘贴上面的地址
然后再复制文件名称,粘贴到函数中。 可是如果就这样的话,会进行报错:
(4)unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
解决方法1:将'\'改成'/'
trainx_source = pd.read_csv('C:/Users/lenovo/Desktop/design.by.wyfxz/hp.csv',header=None )
解决方法2:或者将'\'改成'\\',实现转义
trainx_source = pd.read_csv('C:\\Users\\lenovo\\Desktop\\design.by.wyfxz\\hp.csv',header=None )
解决方法3:在字符串前面加上r或者R
trainx_source = pd.read_csv(r'C:\Users\lenovo\Desktop\design.by.wyfxz\hp.csv',header=None )
trainx_source = pd.read_csv(R'C:\Users\lenovo\Desktop\design.by.wyfxz\hp.csv',header=None )
2024年2月26日补充
我无法让Tensorflow-GPU2.6.0运行。只有降级对我有效,即使这不是最令人满意的方式。执行conda install -c anaconda tensorflow-gpu=2.5
后,所有内容都相应地“更新”
重新搭建TensorFlow-GPU 2.5.0版本