cover

53. TensorFlow Automotive Assessment Classification#

53.1. Introduction#

The previous experiments have learned how to build an artificial neural network using TensorFlow. In this challenge, you will independently complete an open classification prediction exercise.

53.2. Key Points#

  • Building neural networks with TensorFlow

  • Tensor data processing and transformation

  • Loss functions, optimizers

In the experiment of building an artificial neural network with TensorFlow, you have learned the general steps of building a neural network using TensorFlow. In this challenge, the UCI Car Evaluation Dataset is provided. You need to build a neural network by yourself using this dataset and finally complete the training.

First, import this dataset and perform simple processing.

wget -nc https://cdn.aibydoing.com/aibydoing/files/car.data
import pandas as pd

# 加载数据集
df = pd.read_csv("car.data", header=None)
# 设置列名
df.columns = ["buying", "maint", "doors", "persons", "lug_boot", "safety", "class"]
df.head()
buying maint doors persons lug_boot safety class
0 vhigh vhigh 2 2 small low unacc
1 vhigh vhigh 2 2 small med unacc
2 vhigh vhigh 2 2 small high unacc
3 vhigh vhigh 2 2 med low unacc
4 vhigh vhigh 2 2 med med unacc

The dataset contains 6 features and a class target column. The features represent the price and some technical specifications of the car, while the target is the safety assessment of the samples, which is a multi-classification dataset.

The challenge first divides the dataset into a training set and a test set:

from sklearn.model_selection import train_test_split

X = df.iloc[:, :-1]  # 特征
y = df["class"]  # 目标

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

X_train.shape, X_test.shape, y_train.shape, y_test.shape
((1382, 6), (346, 6), (1382,), (346,))

Next, you will independently complete the construction of the neural network and finally obtain the classification accuracy result.

Exercise 53.1

Open-ended Challenge

Challenge: Use TensorFlow to build a reasonable fully-connected artificial neural network to complete the classification task of car safety assessment.

Specification: You need to use the TensorFlow functions and methods learned before to complete the construction, training, prediction, and evaluation of the network. You can choose the data processing method, neural network structure, loss function, optimization method, etc. independently.少量使用其他类库提供的函数及操作 is allowed for non-main code such as data preprocessing.

The open-ended challenge has no thought-guiding process, offering greater freedom and flexibility, and better exercising the ability to solve problems independently. This may involve some knowledge points outside the curriculum, requiring you to have a certain spirit of exploration and the ability to solve problems independently.

Finally, the challenge expects to obtain the test set accuracy and loss results under a reasonable number of iterations. An example is as follows:

Expected Output

Epoch [000/500], Accuracy: [0.17], Loss: [9.9333]
Epoch [100/500], Accuracy: [0.93], Loss: [0.1228]
Epoch [200/500], Accuracy: [0.93], Loss: [0.1056]
Epoch [300/500], Accuracy: [0.93], Loss: [0.1025]
Epoch [400/500], Accuracy: [0.94], Loss: [0.1003]
Epoch [500/500], Accuracy: [0.94], Loss: [0.0992]

Related Links


○ Sharethis article link to your social media, blog, forum, etc. More external links will increase the search engine ranking of this site.