【算法速通】不写一行代码用AI学一百个算法(1/100)——机器学习

不写一行代码用AI学一百个算法(1/100)——机器学习

🚀【探索机器学习的魔法:GPTPT老师的有趣讲解】🤖

“机器学习听起来好高深?不怕!跟着G老师一起轻松入门!🌟 我们不走寻常路,用生活中的简单例子解构这个看似复杂的世界。🧠 首先,我们会用高中数学里的“与或非逻辑”来理解基础概念。然后,G老师将带你一步步解开神秘的神经网络,看看如何通过简单的乘法和加法规则去学习和理解数据。🧩
👀 想知道机器是如何学习的?怎样用神经网络实现基本逻辑门?或者,对于更有挑战性的问题,比如手写数字自动图像识别,我们该如何处理?G老师不仅会教你理论,还会展示实际操作,甚至神经元的可视化!🔍
从基础原理到实际应用,这段视频会让你对机器学习有一个全新的认识。不管你是小白还是对这个领域有一定了解,都能找到新的乐趣和启发。🎓 加入我们,一起探索这个充满可能性的技术世界吧!🌌
🔗 点击观看,开启你的机器学习之旅!🚀”

AI教你《不写一行代码学会数学建模》——机器学习算法

% websave('mnist_train_images.gz', 'http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz');
% websave('mnist_train_labels.gz', 'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz');
% websave('mnist_test_images.gz', 'http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz');
% websave('mnist_test_labels.gz', 'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz');

 %% 步骤2: 解压文件
gunzip('mnist_train_images.gz');
gunzip('mnist_train_labels.gz');
gunzip('mnist_test_images.gz');
gunzip('mnist_test_labels.gz');

% 读取训练图像
fid = fopen('mnist_train_images', 'r', 'b');  % 'b' for big-endian
magic = fread(fid, 1, 'int32');  % Magic number
numImages = fread(fid, 1, 'int32');  % Number of images
rows = fread(fid, 1, 'int32');  % Image height
cols = fread(fid, 1, 'int32');  % Image width

% 读取图像数据
trainImages = fread(fid, inf, 'unsigned char');
trainImages = reshape(trainImages, cols, rows, numImages);
trainImages = permute(trainImages,[2 1 3]);

fclose(fid);

% 读取训练标签
fid = fopen('mnist_train_labels', 'r', 'b');
magic = fread(fid, 1, 'int32');
numLabels = fread(fid, 1, 'int32');

% 读取标签数据
trainLabels = fread(fid, inf, 'unsigned char');

fclose(fid);
layers = [
    imageInputLayer([28 28 1]) % 输入层,图像大小为28x28,1个通道
    fullyConnectedLayer(512) % 全连接层,512个神经元
    reluLayer % ReLU激活函数
    fullyConnectedLayer(10) % 输出层,10个神经元(对应0到9的数字)
    softmaxLayer % softmax层,用于多类别分类
    classificationLayer % 分类层
];

options = trainingOptions('adam', ... % 使用Adam优化器
    'InitialLearnRate',0.001, ... % 调整初始学习率
    'MaxEpochs',10, ...
    'MiniBatchSize', 128, ... % 调整批量大小
    'ValidationFrequency',30, ...
    'Verbose',false, ...
    'Plots','training-progress');

trainImages = reshape(trainImages, [28, 28, 1, 60000]);
trainLabels = categorical(trainLabels);

net = trainNetwork(trainImages, trainLabels, layers, options);


%%
% 提取特定层的激活
% imageIndex = 1; % 选择一个图像索引
% activations = activations(net, trainImages(:,:,:,imageIndex), 'fullyConnectedLayer');

% 可视化激活
% 这里您需要编写代码来可视化activations变量中的数据
%%
predictedLabels = classify(net, trainImages(:,:,:,:));
% 获取所有图像的预测标签


% 获取真实标签
trueLabels = trainLabels;

% 找到所有误判的索引
misclassifiedIndices = find(predictedLabels ~= trueLabels);

% 统计误判情况
numMisclassified = length(misclassifiedIndices);
fprintf('总共 %d 张图像中,有 %d 张被误判。\n', length(trueLabels), numMisclassified);

% 可视化其中一些误判的图像
figure;
for i = 1:min(20, numMisclassified) % 可视化前20张误判的图像
    subplot(4, 5, i);
    imageIndex = misclassifiedIndices(i);
    imshow(trainImages(:,:,:,imageIndex));
    title(sprintf('Predicted: %s\nTrue: %s', string(predictedLabels(imageIndex)), string(trueLabels(imageIndex))), 'Color', 'red');
end
%%
% 创建一个 5x8 的子图
% figure;

for row = 1:5
    for col = 1:8
        % 计算当前图像在 trainImages 中的索引
        imageIndex = (row - 1) * 8 + col +10000;
        
        % 获取当前图像的预测标签
        predictedLabels = classify(net, trainImages(:,:,:,imageIndex));
        
        % 获取当前图像的真实标签
        trueLabel = trainLabels(imageIndex);
        
        % 显示当前图像及其预测标签
        subplot(5, 8, (row - 1) * 8 + col);
        imshow(trainImages(:,:,:,imageIndex));
        title(sprintf('Predicted: %s\nLabel: %s', string(predictedLabels(1)), string(trueLabel)));
    end
end

%%
imageIndex = 1;
predictedLabels = classify(net, trainImages(:,:,:,imageIndex));
figure;
subplot(2,1,1);
imshow(trainImages(:,:,:,imageIndex)); % 显示测试图像
title(sprintf('Predicted: %s ', string(predictedLabels(1))));

% subplot(2,1,2);
%% bar(activations); % 显示输出层的激活
% title('Output Layer Activations');














  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科研GGB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值