Notebook中执行TensorFlow时,Python AttributeError: ‘NoneType‘ object has no attribute ‘xxx‘类型错误的处理

在NoteBook中执行TensorFlow时,Python遇到AttributeError: 'NoneType' object has no attribute 'xxx'类型的报错。

原因分析:Notebook里往往是分段的执行代码逻辑,该错误就是因为相应的引用没有读取到;

解决方案:在对应的执行代码逻辑分段上,添加上引用的逻辑,即添加对应的Import TensorFlow and other libraries。如:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model

code1
code2
code3
code4
code5

另一个报错: ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice_29' (op: 'StridedSlice') with input shapes:

解决方案:将TensorFlow的Tensor转化为numpy.array格式,以numpy.array格式进行类型转化np.float,.astype(bool)和掩码取数组train_data[bool array]。

mask = (data['label'] == 1)

X_train, X_test = train_test_split(data[mask], test_size=0.2, random_state=920)

X_train = X_train.drop(['label'], axis=1).values

X_test = X_test.drop(['label'], axis=1).values

你可能感兴趣的:(TensorFlow,tensorflow,深度学习,机器学习,神经网络)