import pandas as pd import numpy as np from sklearn.preprocessing import LabelEncoder, OneHotEncoder from keras.models import Sequential from keras.layers import Dense from keras.callbacks import History import matplotlib.pyplot as plt # 读取数据集 data = pd.read_csv('data-04-zoo.csv', header=None) # 切分x和y x = data.iloc[:, :-1].values y = data.iloc[:, -1].values # 对y标签进行独热编码处理 label_encoder = LabelEncoder() y = label_encoder.fit_transform(y) onehot_encoder = OneHotEncoder(sparse=False) y = y.reshape(len(y), 1) y = onehot_encoder.fit_transform(y) # 搭建网络模型 model = Sequential() model.add(Dense(16, input_dim=16, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(7, activation='softmax')) # 模型配置 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # 训练模型 history = History() model.fit(x, y, epochs=200, batch_size=16, validation_split=0.2, callbacks=[history]) # 绘制训练集和验证集的损失曲线 plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend(['Train', 'Validation'], loc='upper left') plt.show() # 绘制训练集和验证集的准确率曲线 plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.title('Model Accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.legend(['Train', 'Validation'], loc='upper left') plt.show() # 保存模型 model.save('model1.h5')from google.protobuf.internal import builder as _builder ImportError: cannot import name 'builder' from 'google.protobuf.internal' (C:\ProgramData\anaconda3\envs\demo\lib\site-packages\google\protobuf\internal\__init__.py)
时间: 2023-08-05 11:02:44 浏览: 203
详解pandas数据合并与重塑(pd.concat篇)
5星 · 资源好评率100%
这段代码中出现了 ImportError: cannot import name 'builder' from 'google.protobuf.internal' (C:\ProgramData\anaconda3\envs\demo\lib\site-packages\google\protobuf\internal\__init__.py) 的错误,这可能是因为你的 protobuf 库版本过低导致的。建议更新 protobuf 库到最新版本再尝试运行该代码。可以使用以下命令更新 protobuf 库:
```
pip install --upgrade protobuf
```
如果还有问题,可以参考这个链接进行调试:https://github.com/protocolbuffers/protobuf/issues/9350
阅读全文