1.引子
(或者说水)
最近不是DeepSeek挺火吗,我就想用C++做一个AI。
完整代码在最后,需要自己给AI喂数据,数据不够容易抽风。
2.逻辑
于是我问DeepSeek:解释一下AI的底层逻辑
DeepSeek:
AI的底层逻辑基于数据驱动的数学建模,核心是“学习”数据中的规律。其三大支柱为:
1. 算法(如神经网络)通过调整参数(权重)最小化预测误差;
2. 算力(GPU/TPU)支撑大规模矩阵运算;
3. 数据提供训练素材。训练时,模型通过反向传播优化权重,测试时泛化到新数据。本质是概率统计(如贝叶斯推断)与优化问题(如梯度下降)的结合,最终实现分类、回归或生成任务。
emm...该怎么说呢
于是我问DeepSeek:解释一下AI的底层逻辑,说人话
DeepSeek:
AI的底层逻辑核心原理:
你给AI看100万张猫狗照片(数据),它自己总结出"猫是尖耳朵,狗嘴长"(数学规律),下次看到新照片就能猜。
三大支柱:
数据:AI的"饲料",越多越准
算法:处理数据的"数学公式套餐"
算力:用电脑暴力计算
关键特点:
没有真正的"理解",只是算概率(说"你好"后接"吗"的概率高,就生成"你好吗")
越用越准:像不停刷题的学生,见过的题型越多,解题越熟练
日常类比:
推荐系统=记性超好的售货员(记得你买过酱油,下次推荐醋)
自动驾驶=考了100万次驾照的老司机(但遇到没见过的路况也会懵)
本质上,AI就是用数学模拟神经网络的电信号传递,最终实现:输入→计算→输出。
(满意)
我注意到了其中一句话:没有真正的"理解",只是算概率(说"你好"后接"吗"的概率高,就生成"你好吗")。
我记得这和有本写PyTorch的书对深度学习让AI写诗的说法很像,都是看后接什么字概率最高就写哪个字。
那如何用C++实现呢?
可以做一个嵌套map<string,map<string,int> >,保存词汇A后出现词汇B的次数,输出时查询最可能出现的词汇输出,再递归查询。
那就上代码啊!
3.写代码
我打算用struct封装。
struct AI{
};
先写一些定义:
string s[1000];//存加载的字符串
int k=0,l=0;//计数
string da[1000];map<string,int>mp4;//所有存下的词汇,mp4判断是否出现过
map<string,vector<string> >mp3;//存字符A后出现过的所有字符
map<string,map<string,int> >mp;//重点,mp[s1][s2]表示s1后出现s2的次数
map<string,int>mp2;//求s1出现的次数,输出用
void load(string ss){
s[++k]=ss;//加载数据
}
看注释,不解释了。
接下来就是最难的处理数据了。
首先根据load函数可知总共有k个字符串(这不废话),枚举一遍。
s[i].size()在枚举一遍,每个词语记录链接然后又是一堆map记录,我还加了文件读写。(不说了看代码吧)
void init(){
ifstream in("AI_TALK.in");
// 读取文件中的数据并初始化映射
string word1, word2;
while (in >> word1 >> word2) {
if (!mp4[word1]) {
da[l++] = word1;
mp4[word1] = 1;
}
if (!mp[word1][word2]) {
mp3[word1].push_back(word2);
}
mp[word1][word2]++;
mp2[word1]++;
}
ofstream out("AI_TALK.in",ios::app);
string str="",last="";
for(int i = 1; i<= k;i++){
for(int j = 0; j < s[i].size(); j++){
if(s[i][j]==' '&&last!=""){
//说明已经是第二个词了,可以存链接了
if(!mp4[last]){
da[l++]=last;
mp4[last]=1;
//存词
}
if(!mp[last][str]){
//没出现这种链接就记录
mp3[last].push_back(str);
}
out << last << ' ' << str << endl;
mp[last][str]++;//链接上
mp2[last]++;//记录词出现过
last=str;str="";//往下做
}
else if(s[i][j]==' '){
last=str;str="";
//第一个词,记录
}
else{
str+=s[i][j];
//...不好说,自己理解
}
}
//处理最后的几个词
if(!mp[last][str]){
mp3[last].push_back(str);
}
out << last << ' ' << str << endl;
mp[last][str]++;
mp2[last]++;
last=str;str="";
if(!mp[last][str]){
mp3[last].push_back(str);
}
mp[last][str]++;
mp2[last]++;
}
}
处理了数据就要输出,传入参数开始词汇和期望文章长度,输出开始词汇,枚举寻找出现次数最多的词汇。我输出用的逐字,主要是高端大气上档次。
void print(string c,int t){
for(int i = 0; i < c.size();i++){
cout << c[i];Sleep(10);
}
if(mp2[c]==0){
//没有后继了
return;
}
if(t==0){
//结束了
return;
}
cout << " ";
string maxc="";int maxx=-1;
for(int i = 0; i < mp3[c].size(); i++){
if(maxx<mp[c][mp3[c][i]]){
maxx=mp[c][mp3[c][i]];maxc=mp3[c][i];
}
else if(maxx==mp[c][mp3[c][i]]){
srand(time(NULL));
if(rand()%2==0){
maxx=mp[c][mp3[c][i]];maxc=mp3[c][i];
}
}
}
print(maxc,t-1);
return;
}
这里面之所以有随机数,是因为不同链接出现的次数可能一样,所以随机一个说下去。
于是我开始了测试。
4.debug
没错,这玩意儿有bug。
做完之后,本着好用就往死里用的精神,我让DeepSeek写了几条数据。
a.load("Twinkle , twinkle , little star , How I wonder what you are ! Up above the world so high , Like a diamond in the sky . When the blazing sun is gone , When he nothing shines upon , Then you show your little light , Twinkle , twinkle , all the night .");
a.load("Last summer , I visited Paris with my family . We saw the Eiffel Tower which was very tall and shiny . The Louvre Museum had famous paintings like Mona Lisa . French croissants tasted buttery and delicious . We took a boat ride on the Seine River at sunset . It was my first trip abroad and I will never forget it .");
a.load("My pet cat Mimi is three years old . She has soft gray fur and green eyes . Every morning , she meows for milk and rubs against my legs . After school , we play with a ball of yarn . Sometimes she climbs trees but gets scared to come down . I love Mimi because she is playful and affectionate .");
a.load("Jupiter is the largest planet in our solar system . It has a Great Red Spot which is actually a giant storm . Galileo discovered Jupiter s four largest moons in 1610 . These moons are called Io , Europa , Ganymede , and Callisto . Jupiter s strong gravity helps protect Earth from asteroids .");
a.load("Roses are red , Violets are blue , Sugar is sweet , And so are you . The sunshine is warm , The sky is so clear , Springtime is here , And summer is near . Flowers will bloom , Birds will all sing , Nature s alive , It s a wonderful thing !");
a.load("During winter break , we went skiing in Canada . The snow covered mountains looked like white giants . I learned to ski on the beginner slopes first . My gloves kept my hands warm in the freezing weather . At night , we drank hot chocolate by the fireplace . I hope we can go again next year !");
a.load("Our school library is my favorite place . There are thousands of interesting books on tall shelves . The librarian Mrs . Smith helps us find good stories . I enjoy reading adventure books and science magazines . Every Friday , we have story time with the kindergarten kids . Reading books makes me happy and smart .");
a.load("Mars is called the Red Planet because of iron oxide on its surface . It has the tallest volcano in the solar system named Olympus Mons . Scientists have sent robots called rovers to explore Mars . Maybe one day humans will live on Mars in special domes . Water ice exists at Mars s poles which could support life .");
a.load("My best friend and I do everything together . We sit next to each other in class and share crayons . During recess , we play hopscotch or jump rope . Sometimes we argue but always make up quickly . On weekends , we ride bikes to the park . Friendship means caring and helping each other always .");
a.load("Astronomers use telescopes to study distant galaxies . The Milky Way contains about 100 billion stars . Black holes have such strong gravity that nothing can escape . Nebulas are colorful clouds of gas where new stars form . Light from the Sun takes 8 minutes to reach Earth .");
a.load("The rainbow arches across the sky , A bridge of colors way up high . Red and orange shine so bright , Yellow like the morning light . Green and blue and purple too , After rain the sky renews . Nature paints with colors bold , A masterpiece for young and old .");
a.load("Our family vacation to Thailand was amazing . We saw golden temples with pointy roofs . Long - tail boats took us to beautiful islands . I tried pad thai and loved its sweet taste . Elephants at the sanctuary ate bananas from my hand . The warm ocean water was perfect for swimming .");
a.load("My classroom has twenty desks and a big whiteboard . We learn math , science , and English every day . During art class we paint with watercolors . The playground has swings , slides , and a soccer field . School teaches me new things and helps me make friends .");
a.load("Saturn s rings are made of ice and rock particles . They stretch over 280 , 000 kilometers wide . Titan , Saturn s largest moon , has lakes of methane . The Cassini spacecraft studied Saturn for 13 years . Saturn is so light it could float in water .");
a.load("Little raindrops on my window , Tap - tap - tapping all day long . Some are fast and some are slow , Singing nature s gentle song . Puddles form upon the ground , Reflecting clouds up in the sky . Soon the sun comes shining round , Making rainbows way up high .");
a.load("Visiting New York City was exciting but busy . We rode to the top of the Empire State Building . Times Square at night glowed with bright lights . Central Park had horse carriages and street performers . The subway trains were crowded but fast . I want to go back to see more museums .");
a.load("Recycling helps protect our planet Earth . We separate paper , plastic , and cans at school . Turning off lights saves electricity . Planting trees gives animals homes . Walking instead of driving reduces pollution . Everyone can help keep nature clean .");
a.load("Venus is the hottest planet in our system . Its thick clouds trap heat like a blanket . A day on Venus is longer than its year . The surface has volcanoes and cracked plains . Venus spins backwards compared to other planets .");
a.load("My grandmother tells the best stories . She remembers when she was a little girl . Her house smells like cookies and lavender . We bake pies together on weekends . She teaches me old songs and games . Grandparents are special because they have so much love .");
对,亿条。
一测就发现问题了,他的回答长这样。
The surface has volcanoes and a little girl . The surface has volcanoes and a little girl . The surface has volcanoes and a little star , we play with my family . The Louvre Museum had famous paintings like Mona Lisa . The surface has volcanoes and a little girl . The surface has volcanoes and I wonder what you are ! Up above the sky . The Louvre Museum had famous paintings like Mona Lisa . The Louvre Museum had famous paintings like Mona Lisa . The surface has volcanoes and a little girl .
发现了没有?他有很多重复的The surface has volcanoes and a little girl .(不要管他的意思,这个AI比较低级)。
要怎么解决呢?
看下篇吧。
都看到这了,关个注吧,实在不行点个赞。
对了,代码。
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
const int N=1e5+10;
struct ai{
string s[N];//存加载的字符串
int k=0,l=0;//计数
string da[N];map<string,int>mp4;
map<string,vector<string> >mp3;
map<string,map<string,int> >mp;
map<string,int>mp2;
void load(string ss){
s[++k]=ss;
}
void init(){
ifstream in("AI_TALK.in");
// 读取文件中的数据并初始化映射
string word1, word2;
while (in >> word1 >> word2) {
if (!mp4[word1]) {
da[l++] = word1;
mp4[word1] = 1;
}
if (!mp[word1][word2]) {
mp3[word1].push_back(word2);
}
mp[word1][word2]++;
mp2[word1]++;
}
ofstream out("AI_TALK.in",ios::app);
string str="",last="";
for(int i = 1; i<= k;i++){
for(int j = 0; j < s[i].size(); j++){
if(s[i][j]==' '&&last!=""){
//说明已经是第二个词了,可以存链接了
if(!mp4[last]){
da[l++]=last;
mp4[last]=1;
//存词
}
if(!mp[last][str]){
//没出现这种链接就记录
mp3[last].push_back(str);
}
out << last << ' ' << str << endl;
mp[last][str]++;//链接上
mp2[last]++;//记录词出现过
last=str;str="";//往下做
}
else if(s[i][j]==' '){
last=str;str="";
//第一个词,记录
}
else{
str+=s[i][j];
//...不好说,自己理解
}
}
//处理最后的几个词
if(!mp[last][str]){
mp3[last].push_back(str);
}
out << last << ' ' << str << endl;
mp[last][str]++;
mp2[last]++;
last=str;str="";
if(!mp[last][str]){
mp3[last].push_back(str);
}
mp[last][str]++;
mp2[last]++;
}
}
void print(string c,int t){
/*Eglish*/
for(int i = 0; i < c.size();i++){
cout << c[i];Sleep(10);
}
if(mp2[c]==0){
//没有后继了
return;
}
if(t==0){
//结束了
return;
}
cout << " ";
string maxc="";int maxx=-1;
for(int i = 0; i < mp3[c].size(); i++){
if(maxx<mp[c][mp3[c][i]]){
maxx=mp[c][mp3[c][i]];maxc=mp3[c][i];
}
else if(maxx==mp[c][mp3[c][i]]){
srand(time(NULL));
if(rand()%2==0){
maxx=mp[c][mp3[c][i]];maxc=mp3[c][i];
}
}
}
print(maxc,t-1);
return;
}
};
int main(){
ai a;
a.init();srand(time(NULL));
a.print("The",100);
return 0;
}
(复制我前面的load)
下一篇:C++实现AI智能体2