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 li
在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

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)