定制加工
ChatGPT:你才是编译器!你全家都是编译器!
2024-12-31 16:47

点击上方小白学视觉”,选择加"星标"或“置顶

重磅干货,第一时间送达

我是不是再也不需要编译器了?!


这个故事的灵感来自一个类似的文章:在 ChatGPT 中构建虚拟机。我印象深刻并决定尝试类似的东西,但这次不是 Linux 命令行工具,而是让 ChatGPT 成为我们的 Python 编译器。


这是初始化 ChatGPT 的命令:

I want you to act as a Python interpreter. I will type commands and you will reply with what thepython output should show. I want you to only reply with the terminal output inside one uniquecode block, and nothing else. Do no write explanations, output only what python outputs. Do not type commands unless Iinstruct you to do so. When I need to tell you something in English I will do so by puttingtext inside curly brackets like this: {example text}. My first command is a=1.

看起来似乎很好用,让我们尝试一些简单的算术表达式。

同样可以运行,如果我们使用未导入的库会发生什么?

好吧,它试图提示我有一个错误。我其实并不希望它这样做,所以我会再次要求他不要输出任何东西,除了 Python 代码。

{Print only python output, do not print any comments}

table-paragraph="">仅作记录,ChatGPT 有时能够使用未导入的库,但这次我很幸运,它会打印一条错误消息。


好吧,我很确定 ChatGPT 能够完成简单的任务,让我们尝试更复杂的事情,让它输出二进制搜索算法的结果。

# Binary Search in pythondef binarySearch(array, x, low, high):
# Repeat until the pointers low and high meet each other while low <= high:
mid = low + (high - low)//2
if array[mid] == x: return mid
elif array[mid] < x: low = mid + 1
else: high = mid - 1
return -1

array = [3, 4, 5, 6, 7, 8, 9]x = 4
result = binarySearch(array, x, 0, len(array)-1)
if result != -1: print("Element is present at index " + str(result))else: print("Not found")

好像不想听我的要求只输出Python,但是输出还是正确的,厉害!


让我们尝试输入一个不存在的数字,比如:

x = 4.5

让我们跳进更复杂的东西。让我们从一些简单的机器学习算法开始,比如线性回归。我想知道 ChatGPT 是否能够解决一个简单的优化任务…

import numpy as npimport matplotlib.pyplot as plt def estimate_coef(x, y):   # number of observations/points   n = np.size(x)    # mean of x and y vector   m_x = np.mean(x)   m_y = np.mean(y)    # calculating cross-deviation and deviation about x   SS_xy = np.sum(y*x) - n*m_y*m_x   SS_xx = np.sum(x*x) - n*m_x*m_x    # calculating regression coefficients   b_1 = SS_xy / SS_xx   b_0 = m_y - b_1*m_x    return (b_0, b_1) def plot_regression_line(x, y, b):   # plotting the actual points as scatter plot   plt.scatter(x, y, color = "m",              marker = "o", s = 30)    # predicted response vector   y_pred = b[0] + b[1]*x    # plotting the regression line   plt.plot(x, y_pred, color = "g")    # putting labels   plt.xlabel('x')   plt.ylabel('y')    # function to show plot   plt.show() def main():   # observations / data   x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])   y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])    # estimating coefficients   b = estimate_coef(x, y)   print("Estimated coefficients:\nb_0 = {}  \         \nb_1 = {}".format(b[0], b[1]))    # plotting regression line   # plot_regression_line(x, y, b) if __name__ == "__main__":   main()

此任务的正确答案是:

Estimated coefficients:b_0 = 1.2363636363636363        b_1 = 1.1696969696969697

ChatGPT 的输出是:

这很接近真实值!如果我们在 Python 中绘制预测,我们将得到下图:

关于这个任务的另一个有趣的事实是,我又运行了一次相同的代码,输出与真实值完全吻合。因此,我们可以认为这个任务通过了。


好吧,是时候学习一些简单的神经网络了!也许我们可以拟合一个简单的 Keras 模型?

# first neural network with keras make predictionsfrom numpy import loadtxtfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Dense# load the datasetdataset = loadtxt('pima-indians-diabetes.csv', delimiter=',')# split into input (X) and output (y) variablesX = dataset[:,0:8]y = dataset[:,8]# define the keras modelmodel = Sequential()model.add(Dense(12, input_shape=(8,), activation='relu'))model.add(Dense(8, activation='relu'))model.add(Dense(1, activation='sigmoid'))# compile the keras modelmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])# fit the keras model on the datasetmodel.fit(X, y, epochs=150, batch_size=10, verbose=0)# make class predictions with the modelpredictions = (model.predict(X) > 0.5).astype(int)# summarize the first 5 casesfor i in range(5):print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))

请注意,数据集实际上是一个 CSV 文件,ChatGPT 无权访问该文件。

好吧,这是正确的输出,我很害怕。如果我将网络架构更改为不正确的架构,会发生什么情况?


让我们改变输入尺寸:

model.add(Dense(12, input_shape=(6,), activation='relu'))

哈!看来我离失业还有几年的时间;这次 ChatGPT 没看懂,还是输出了正确结果。


好的,让我们做最后一个任务,在 OpenAI 中调用 Huggingface 怎么样?

正确输出:

[{'entity_group': 'ORG',  'score': 0.9472818374633789,  'word': 'Apple',  'start': 0,  'end': 5}, {'entity_group': 'PER',  'score': 0.9838564991950989,  'word': 'Steve Jobs',  'start': 74,  'end': 85}, {'entity_group': 'LOC',  'score': 0.9831605950991312,  'word': 'Los Altos',  'start': 87,  'end': 97}, {'entity_group': 'LOC',  'score': 0.9834540486335754,  'word': 'Californie',  'start': 100,  'end': 111}, {'entity_group': 'PER',  'score': 0.9841555754343668,  'word': 'Steve Jobs',  'start': 115,  'end': 126}, {'entity_group': 'PER',  'score': 0.9843501806259155,  'word': 'Steve Wozniak',  'start': 127,  'end': 141}, {'entity_group': 'PER',  'score': 0.9841533899307251,  'word': 'Ronald Wayne',  'start': 144,  'end': 157}, {'entity_group': 'ORG',  'score': 0.9468960364659628,  'word': 'Apple Computer',  'start': 243,  'end': 257}]

ChatGPT 输出:

[{'word': 'Apple', 'score': 0.9993804788589478, 'entity': 'I-ORG'}, {'word': 'Steve', 'score': 0.999255347251892, 'entity': 'I-PER'}, {'word': 'Jobs', 'score': 0.9993916153907776, 'entity': 'I-PER'}, {'word': 'Steve', 'score': 0.9993726613044739, 'entity': 'I-PER'}, {'word': 'Wozniak', 'score': 0.999698519744873, 'entity': 'I-PER'}, {'word': 'Ronald', 'score': 0.9995181679725647, 'entity': 'I-PER'}, {'word': 'Wayne14', 'score': 0.9874711670837402, 'entity': 'I-PER'}, {'word': 'Apple', 'score': 0.9974127411842163, 'entity': 'I-ORG'}, {'word': 'Computer', 'score': 0.968027651309967, 'entity': 'I-ORG'}, {'word': 'Apple', 'score': 0.8259692192077637, 'entity': 'I-ORG'}]

table-paragraph="">结果接近 huggingface 的输出结果。我的猜测是 Huggingface API 发生了变化,并且由于 ChatGPT 没有接受过最新历史数据的训练,它以旧格式输出结果。


总 结


最近几天我一直在玩 ChatGPT,我对使用这个工具具有的无限可能性着迷。虽然它不是真正的 Python 编译器,但它在为我编译 Python 代码方面仍然做得很好。我还发现它很好地解决了 HARD leetcode 问题!


并得出结论:

chat gpt how will you help the humanity?

如果您还没有尝试过 ChatGPT,那您绝对应该尝试,这就是未来!

·  END  ·

好消息!

小白学视觉知识星球

开始面向外开放啦

    以上就是本篇文章【ChatGPT:你才是编译器!你全家都是编译器!】的全部内容了,欢迎阅览 ! 文章地址:http://ww.kub2b.com/news/17359.html
     栏目首页      相关文章      动态      同类文章      热门文章      网站地图      返回首页 企库往资讯移动站 http://ww.kub2b.com/mobile/ , 查看更多   
最新文章
光威复材2024年下降逾15%,连续两年发生下滑
3月31日晚,(300699.SZ)发布2024年财报,实现营收24.5亿元,同比下降2.69%;归母净利润7.41亿元,同比下降15.12%;扣非净利润6.6
看完超人气游戏改编的《我的世界大电影》,我整个人都方了
观影《我的世界大电影》,感觉自己真的方了。毕竟,已经领略过风靡全球的沙盒游戏《我的世界》中那方块的世界,还有游戏带给人的
手机大脑手机阅读「手机大脑」
为什么只要醒着我们就离不开手机,为什么疫情中的我们更容易自投罗网?为什么比尔·盖茨不让小孩用手机,乔布斯不让孩子碰iPad?
远洋集团(03377):拟3.22亿元出售北京盛永置业投资23%股权
智通财经APP讯,远洋集团(03377)发布公告,于2025年4月11日,卖方(公司全资附属公司北京银港房地产开发有限公司)拟向买方(日照钢
600375,停牌,退市风险解除!
*ST汉马(600375)即将去星摘帽。4月16日晚间,该公司公告,公司股票将于2025年4月17日(星期四)开市起停牌一天,并于2025年4月
300万像素手写滑盖三星G618行货大跌530三星滑盖手机「300万像素手写滑盖三星G618行货大跌530」
  【7月15日太平洋电脑网上海站】今天,三星奥运手机G618行货从2288元跌至新低1758元。530元的降幅对于这款三星众多奥运手机中
最新手机续航TOP10排行榜:小米14垫底,Mate60和iPhone15落榜,第一名让人很意外手机续航排行「最新手机续航TOP10排行榜:小米14垫底,Mate60和iPhone15落榜,第一名让人
随着科技的进步,手机已经成为了我们生活中不可或缺的一部分。而手机续航能力也成为用户选择新机的重要考量因素之一。特别是对于
“好房子”新规激发楼市活力 北京3月新旧房成交量齐增
每经记者:陈梦妤    每经编辑:魏文艺“今年第一季度,我们所有项目均超额完成销售任务,完成率均达100%以上。这一成绩主要
智能手机:重塑现代人生活方式的双刃剑现代手机「智能手机:重塑现代人生活方式的双刃剑」
随着科技的飞速发展,智能手机已经成为现代人生活中不可或缺的一部分。它为我们带来了便捷的信息获取、高效的社交方式以及
10块钱的VR跟7000元的到底有啥区别?部手机「10块钱的VR跟7000元的到底有啥区别?」
步入VR元年,VR的关注度甚至乎呈直线上升,但是还有很多VR小白根本不知道VR为何物。VR频道特设《小白玩VR》系列文章,没看过的,