Python学习路上遇见的各种错

编码

错误:AttributeError: ‘float’ object has no attribute ‘decode’
来源:jieba.lcut(x)
修改:jieba.lcut(str(x))

LSTM输出问题

错误:ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2
来源:

model.add(LSTM(units=64, activation='tanh', inner_activation='hard_sigmoid'))
model.add(Dropout(0.5))
model.add(LSTM(units=64, activation='tanh'))
model.add(Dense(3, activation='softmax'))

修改:除了最后一个LSTM,其他的都加上 return_sequences=True

model.add(LSTM(units=64, activation='tanh', inner_activation='hard_sigmoid',return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(units=64, activation='tanh'))
model.add(Dense(3, activation='softmax'))

原因:

  • 输入shape
    • 形如(samples,timesteps,input_dim)的3D张量
  • 输出shape
    • 如果return_sequences=True:返回形如(samples,timesteps,output_dim)的3D张量
    • 否则,返回形如(samples,output_dim)的2D张量

graphviz安装问题

  • 错误:pydot failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.
  • 来源:我的电脑系统为deepin15.9.3,使用pyplot可视化keras的Sequential模型结构,所以需要安装graphviz
  • 解决:在终端输入 sudo apt install graphviz

vscode中当前路径和相对路径问题

  • 问题:FileNotFoundError: [Errno 2] File b’./data/neg.csv’ does not exist: b’./data/neg.csv’
  • 情况:该文件所在文件夹:/home/zmh/Documents/learn/python/path1/fatherPath
    • fatherPath
      • data
        • neg.csv
        • pos.csv
      • LSTM
        • lstm.py
          按照常理在这个文件中使用相对路径 ‘./data/neg.csv’ 是可以访问到对应文件的,但是不管怎么尝试都不对,后来做了个测试,输出的路径是在vscode打开的一级目录。所有的相对目录都是相对于vscode打开文件/home/zmh/Documents/learn/python而言的
import os
cur_path = os.getcwd()
print(cur_path)
# 输出:/home/zmh/Documents/learn/python

pandas读取csv文件

  • 错误:pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 911,saw 2.
  • 来源:
pos = pd.read_csv(pos_path,header=None,index_col=None)
  • 解决:添加 error_bad_lines=False
pos = pd.read_csv(pos_path,header=None,index_col=None,error_bad_lines=False)

你可能感兴趣的:(Python)