shields 徽标_神经网络生成超级英雄徽标

shields 徽标

Machine Learning is a superpower in the computer science world. We can use it to predict the future, make robots see by detecting and classifying objects, or to find products and content that fit our tastes, thanks to recommendation systems.

机器学习是计算机科学领域的超级大国。 借助推荐系统,我们可以使用它来预测未来,通过检测和分类对象使机器人看到,或者找到适合我们口味的产品和内容。

When more powerful Deep Learning methods were discovered, we got tools capable of achieving almost magical results. Generating artificial images looking like real ones, or transferring style to generate own versions of masterpieces sparked our imagination and creativity.

当发现更强大的深度学习方法时,我们得到了能够实现近乎神奇的结果的工具。 生成看起来像真实图像的人造图像,或传递样式以生成自己的杰作版本,激发了我们的想象力和创造力。

Creative ways of using Neural Networks stimulates our motivation and learning process. Even though we have advanced Generative Models, there are ways of using simple, classic, multilayer Neural Networks to generate visual content and use it to depict how the model works. So let’s go creative and generate our own superhero logo!

使用神经网络的创新方法激发了我们的动力和学习过程。 即使我们拥有先进的生成模型,也有使用简单,经典,多层神经网络生成视觉内容并使用它来描述模型工作方式的方法。 因此,让我们发挥创意并生成我们自己的超级英雄徽标!

数据优先 (Data first)

Data Science models need to be fed by data. That core component defines the model’s behavior and is a basis for artificial brains. The seeds of our own superhero logo will be coded in a traditional, two-dimensional grid of points.

数据科学模型需要由数据提供。 该核心组件定义了模型的行为,是人工大脑的基础。 我们自己的超级英雄徽标的种子将以传统的二维点网格编码。

627 blue points.
2D grid of points (image by Author).
点的二维网格(作者提供的图像)。

That’s our training data set. It consists of 627 points with x,y coordinates, and a label. There are two types of labels 0 and 1.

那就是我们的训练数据集。 它由具有x,y坐标的627个点一个标签组成 。 标签有两种类型: 01

What is represented in this data? I hope my Neural Network will show us after it’s properly trained, but let’s introduce our model first.

此数据代表什么? 我希望我的神经网络在经过适当训练后能向我们展示,但让我们首先介绍一下我们的模型。

神经网络上尉 (Captain Neural Network)

The idea is quite simple. We’ll use a 3-layered Neural Network [1]. It consists of an input layer, that just passes data points to a hidden layer. Here we have neurons with sigmoid activation function:

这个想法很简单。 我们将使用三层神经网络 [1]。 它由一个输入层组成, 该输入层仅将数据点传递到隐藏层 。 这里我们有具有乙状结肠激活功能的神经元:

The formula of sigmoid function.
Sigmoid activation function (image by Author).
乙状结肠激活功能(作者提供的图像)。

Neurons from the hidden layer have 2 weights and a bias:

来自隐藏层的神经元具有2个权重和一个偏差:

Scheme of a hidden layer neuron. It has two inputs and a bias and a sigmoid activation function that produces output.
Hidden layer neuron scheme (image by Author).
隐藏层神经元方案(作者提供的图片)。

Finally, the last layer will use outputs from hidden layer neurons and produce the label for our points. As we have only two possible labels one output neuron is sufficient.

最后,最后一层将使用隐藏层神经元的输出,并为我们的点生成标签。 由于我们只有两个可能的标签, 一个输出神经元就足够了。

Take a look at our model:

看一下我们的模型:

3-layered Neural Network. 2 input neurons, 3 hidden neurons and one output neuron. 2 bias neurons.
3-layered Neural Network (image by Author).
三层神经网络(作者提供)。

训练任务 (Training mission)

Having our data and the Neural Network model we can start the training process. Let’s try out the simplest approach:

有了我们的数据和神经网络模型,我们就可以开始训练过程。 让我们尝试最简单的方法:

  1. Use data as it is.

    照原样使用数据。
  2. Put 40 hidden neurons.

    放置40个隐藏的神经元。

  3. Initialize neurons weights using random values close to 0, from the range (-0.2, 0.2).

    使用范围(-0.2,0.2)中接近0的随机值初始化神经元权重。

  4. Present each data point 1 000 times (1 000 training iterations).

    每个数据点显示1 000次(1 000次训练迭代)。

After the training process, we’ll ask our Neural Network to assign labels for each data point from the training set. Then we’ll plot the points using different colors for labels 0 and 1. If the model learned patterns from our data we should see a superhero logo.

训练过程结束后,我们将要求神经网络为训练集中的每个数据点分配标签。 然后,我们将为标签01使用不同的颜色绘制点。 如果模型从我们的数据中学到了模式,我们应该会看到一个超级英雄徽标。

Let’s check it:

让我们检查一下:

Grid of points. Points in the center are blue, the rest of points is yellow.
Neural Network failed to learn the pattern (image by Author).
神经网络无法学习该模式(作者提供的图像)。

Oops, it doesn’t look good. The neural network didn’t get the idea included in data pretty well.

糟糕,看起来不太好。 神经网络没有很好地将想法包含在数据中。

What’s the root cause? There are a couple of possible issues. First of all, our data is not normalized. We feed the Network with coordinates that range from -8.0 to 8.0. As we use the sigmoid activation function it should work better for data in range (0, 1).

根本原因是什么? 有两个可能的问题。 首先,我们的数据未标准化 。 我们使用-8.08.0范围内的坐标来投放网络 当我们使用S型激活函数时,它应该更好地适用于范围(0,1)的数据。

However, looking at the mean squared error [2], and its decreasing trend we can also try to increase the number of training iterations:

但是,查看均方误差 [2]及其下降趋势,我们还可以尝试增加训练迭代次数:

Mean squared error plot with decreasing trend.
Mean squared error per iteration (image by Author).
每次迭代的均方误差(作者提供的图像)。

更多迭代 (More iterations)

To check if the Network needs more time to learn the pattern let’s give it a chance. Let it run wild for 40 000 iterations and see what happens.

要检查网络是否需要更多时间来学习该模式,请给它一个机会。 让它疯狂运行40 000次迭代,看看会发生什么。

Blue points which looks like a bat, surrounded by yellow points.
Properly classified training set (image by Author).
正确分类的训练集(作者提供的图像)。

Can you see something familiar in the picture? Think of one of the most famous comic book issue characters. The one who is quite dark and enjoys working at nights. Let me help you:

您可以在图片中看到一些熟悉的东西吗? 想想最著名的漫画发行人物之一。 那个很黑,喜欢晚上工作的人。 让我来帮助你:

Blue points are surrounded by a Batman curve. It looks like a Bat logo.
Classified training set and “Batman curve” (image by Author).
分类训练集和“蝙蝠侠曲线”(作者提供)。

Yes, it’s the Batman logo!

是的,这是蝙蝠侠的标志!

蝙蝠侠曲线 (Batman curve)

The mystery behind my dataset is already revealed. I built it using these formulas [3]:

我的数据集背后的奥秘已经揭晓。 我使用以下公式构建它[3]:

Formulas needed to draw a Batman curve.
“Batman curve” formulas (image by Author).
“蝙蝠侠曲线”公式(作者提供的图像)。

How exactly I used it? My code iterates over points through a coordinates system with a small step (0.1). If the point was inside the curve it was labeled 1. Otherwise, points were labeled as 0.

我到底是怎么使用的? 我的代码以很小的步长(0.1)通过坐标系统遍历点。 如果该点在曲线内,则标记为1 。 否则,点被标记为0。

The Neural Network learned how to distinguish points inside the curve from the points outside of it, that’s great. However, there are two issues:

神经网络学习了如何区分曲线内的点与曲线外的点,这很棒。 但是,有两个问题:

  1. It’s not my own logo, the Batman symbol is already taken.

    这不是我自己的徽标,蝙蝠侠符号已经被使用。
  2. The Neural Network did well on the training set, which is not a big deal.

    神经网络在训练集上表现不错,这没什么大不了的。

It’s time to solve both of these issues.

现在是解决这两个问题的时候了。

测试任务 (Test mission)

To have the original logo generated by a Neural Network, I’ll generate a second grid of points. However this time it will be a really thick grid. What’s more, there will be no labels assigned to each point.

为了获得由神经网络生成的原始徽标,我将生成第二个点网格。 但是这一次它将是一个非常厚的网格。 而且,不会为每个点分配标签。

Instead, already trained Neural Network will be used to generate labels for a thicker grid. This time grid consists of 26 000 points. The probability that the given point wasn’t a part of a training set (627 points) is quite high.

相反,已经训练有素的神经网络将用于为较厚的网格生成标签。 该时间网格包含26000点。 给定点不属于训练集的一部分(627点)的可能性非常高。

After iterating over each point and getting the label predicted by Neural Network we can plot the result:

遍历每个点并获得神经网络预测的标签后,我们可以绘制结果:

A lot of blue and yellow points. Blue points look like a bird or an elephant head.
New logo generated by the Neural Network (image by Author).
神经网络生成的新徽标(作者提供的图像)。

We have it! But which superhero could use it? A flying squirrel man? Or maybe it’s more like an elephant man? I’ll leave it to your imagination.

我们有它! 但是哪个超级英雄可以使用它? 一只松鼠人? 还是更像一个大象人? 我让你想象。

生成徽标的全过程 (The full process of generating the logo)

Let’s summarize how to generate the logo:

让我们总结一下如何生成徽标:

  1. Find a pattern or a formula that will be used as a basis of your image. Use points’ coordinates as inputs for your neural network. Different labels for different areas are crucial for your final image. Make the training set grid not so thick, so your Neural Network has some space to be creative in unknown areas.

    查找将用作图像基础的图案或公式。 使用点的坐标作为神经网络的输入。 不同区域的不同标签对于最终图像至关重要。 使训练集的网格不要太厚,以便您的神经网络在未知区域中有一定的创造空间。

  2. Generate a much thicker grid as your test set. This time don’t label these points.

    生成更厚的网格作为测试集。 这次不要标记这些点。
  3. Train your Neural Network.

    训练您的神经网络。
  4. Generate labels for test set points and plot points with different labels using different colors.

    生成测试设置点的标签,并使用不同的颜色绘制带有不同标签的点。
  5. Check the result. If you don’t like it, play with the Neural Network parameters and see how it affects the final result.

    检查结果。 如果您不喜欢它,请使用神经网络参数,看看它如何影响最终结果。

The Octave code as well as data used to run these experiments are available here [4].

八度代码以及用于运行这些实验的数据可在此处获得[4]。

Take a look at three examples of “Batman curve based” logos generated by playing with the Neural Network parameters:

看一下通过玩神经网络参数生成的“基于蝙蝠侠曲线”徽标的三个示例:

Image for post
Image for post
Image for post
Logos generated by different Neural Networks (images by Author).
由不同的神经网络生成的徽标(作者提供的图像)。

好玩 (Make it fun)

Data Science and learning can be fun. By finding creative ways of using science you encourage yourself to learn more and you get a better understanding of how things work.

数据科学和学习可能很有趣。 通过寻找创新的科学使用方式,您可以鼓励自己学习更多,并更好地了解事物的工作方式。

Visual forms are even more appealing to our imagination, as we use different senses to get most of the things we’re trying to accomplish or learn. You are welcome to change a number of neurons, desired error, as well as many other parameters and immediately see how it affects the final result.

视觉形式对我们的想象力更具吸引力,因为我们使用不同的感官来获取我们试图完成或学习的大多数事情。 欢迎您更改许多神经元,所需的错误以及许多其他参数,并立即查看它如何影响最终结果。

Remember, with great power comes great responsibility. If you come up with an idea that can spark curiosity don’t hesitate to share it!

请记住,强大的力量伴随着巨大的责任。 如果您提出一个可以激发好奇心的想法,请立即分享!

参考书目: (Bibliography:)

  1. Joel Grus, Data Science from Scratch, 2nd Edition, ISBN: 978–1492041139

    Joel Grus,《 数据科学从零开始》,第二版 ,ISBN:978–1492041139

  2. https://en.wikipedia.org/wiki/Mean_squared_error

    https://zh.wikipedia.org/wiki/均方误差

  3. https://www.desmos.com/calculator/dnzfajfpym

    https://www.desmos.com/calculator/dnzfajfpym

  4. https://github.com/rauluka/mluvr-logo

    https://github.com/rauluka/mluvr-logo

翻译自: https://towardsdatascience.com/neural-network-generating-superhero-logo-c86f931c9cf1

shields 徽标

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
需要注意的是,这段代码中的字符串 `'` 可能是中文输入法下的特殊字符,会导致程序出错。建议将其替换为 `'` 或 `"`。 具体来说,代码的实现过程如下: 1. 读入 n、s 和 k,以及两个列表 d 和 v,分别表示敌人距离和敌人的价值。 2. 将所有敌人按照价值从高到低排序,记录每个敌人向左和向右扩展 k 的范围内价值最高的敌人。 3. 枚举所有可能的护盾组合,计算每个组合下的总价值,找到最优组合以及对应的最大价值。 4. 输出最优组合以及对应的最大价值。 具体实现过程中,算法先将所有敌人按照价值从高到低排序: ```python nodes = sorted([(i, v[i]) for i in range(n)], key=lambda x: x[1], reverse=True) ``` 然后对每个敌人向左和向右分别扩展 k 的范围,记录每个范围内价值最高的敌人: ```python left_max = [(-1, -1)] * n right_max = [(-1, -1)] * n for i, _ in nodes: # 向左扩展 j = i while j >= 0 and d[i-j] <= k: if v[j] > left_max[i][1]: left_max[i] = (j, v[j]) j -= 1 # 向右扩展 j = i while j < n and d[j-i] <= k: if v[j] > right_max[i][1]: right_max[i] = (j, v[j]) j += 1 ``` 最后枚举所有可能的护盾组合,计算每个组合下的总价值,找到最优组合以及对应的最大价值: ```python max_value = -1 max_shields = [] for i in range(s+1): shields = nodes[:i] value = sum([v[i] for i in range(n) if any([abs(i-j) <= k for j, _ in shields])]) if value > max_value: max_value = value max_shields = shields max_shields.sort() ``` 最后输出最优组合以及对应的最大价值: ```python print(' '.join([str(i) for i, _ in max_shields])) print(max_value) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值