保持简单

I have delved into learning about neural networks at least a dozen times, and learned a bit more each time. This time I want to start off with the simplest datasets and use them to understand network behavior. This article is the first in a series on my adventures with neural networks.

我至少学习了十几次关于神经网络的知识,并且每次都学到更多。 这次,我想从最简单的数据集开始,并使用它们来了解网络行为。 本文是我在神经网络上的冒险系列的第一篇。

In this post, we are going to focus on multiclass classification. This is the problem of trying to identify which class a particular data point belongs to, from a limited set of classes.

在这篇文章中,我们将专注于多类分类。 这是试图从一组有限的类中识别特定数据点属于哪个类的问题。

The libraries we will use are tensorflow , numpy , matplotlib, and sklearn :

我们将使用的库是tensorflownumpymatplotlibsklearn

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as pltfrom sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split

We will start off by making two clusters of data:

我们将从制作两个数据簇开始:

X, y = make_blobs(n_samples=1000, centers=2, n_features=2, random_state=0, cluster_std=0.9)
y = tf.keras.utils.to_categorical(y)
plt.scatter(X[:,0], X[:,1])

Let’s split the data into training and test sets:

让我们将数据分为训练集和测试集:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

Now for the actual model. We are going to build a multi-layer perceptron. The four layers will be Dense layers, with the last layer having a softmax activation function that will squash the predictions into a probability distribution that can be used to make a prediction between classes:

现在为实际模型。 我们将构建一个多层感知器。 这四个层将是密集层,最后一层具有softmax激活函数,该函数将把预测压缩为概率分布,该概率分布可用于在类之间进行预测:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(16, input_shape=(2,), activation="relu"))
model.add(tf.keras.layers.Dense(16, activation="relu"))
model.add(tf.keras.layers.Dense(16, activation="relu"))
model.add(tf.keras.layers.Dense(2, activation="softmax"))model.summary()
Image for post
Summary of our model
模型总结

Next is the model compilation step. We will use the Adam optimizer, and categorical_crossentropy to calculate loss in our multi-class classification problem. And we will measure the accuracy of our model as a metric.

接下来是模型编译步骤。 我们将使用Adam优化器和categorical_crossentropy来计算我们的多类分类问题中的损失。 我们将以模型衡量模型的准确性。

model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"]
)

Let’s run 10 epochs:

让我们运行10个时代:

history = model.fit(
X_train,
y_train,
epochs=10,
validation_data=(X_test, y_test)
)

Finally, we can take a look at the accuracy of the model on the training and testing data:

最后,我们可以在训练和测试数据上查看模型的准确性:

_, train_accuracy = model.evaluate(X_train, y_train, verbose=0)
_, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print("Train: %.3f, Test: %.3f" % (train_accuracy, test_accuracy))>> Train: 0.957, Test: 0.948

On the training data, our model has 95.7% accuracy, and 94.8% on the test data. This makes sense, since the model has seen the training data before. The test accuracy is the real measure of how good our model is at predicting.

在训练数据上,我们的模型具有95.7%的准确性,在测试数据上具有94.8%的准确性。 这是有道理的,因为该模型之前已查看过训练数据。 测试准确性是我们的模型预测的良好程度的真实度量。

What happens if we change the hyperparameters of the model? Hyperparameters include things like the number of epochs, number and types of hidden layers, and the activation functions. I will explore all of these in future posts.

如果我们更改模型的超参数会发生什么? 超参数包括纪元数,隐藏层的数量和类型以及激活功能。 我将在以后的文章中探讨所有这些内容。

翻译自: https://medium.com/@the.ganesh.ravichandran/keeping-it-simple-bb8032d85d5c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值