用一两行代码提醒训练完成

github

Knock Knock

A small library to get a notification you when your training is complete or when it crashes during the process with two additional lines of code.

When training deep learning models, it is common to use early stopping. Apart from a rough estimate, it is difficult to predict when the training will finish. Then, it can be interesting to set up automatic notifications for your training. It is also interesting to be notified when your training crashes in the middle of the process for unexpected reasons.

Installation

Install with pip or equivalent.

pip install .

This code has only been tested with Python 3.6.

Usage

The library is designed to be used in a seamless way, with minimal code modification: you only need to add a decorator on top your main function call.

There are currently three ways to setup notifications: email, Slack and Telegram.

Email

The service relies on Yagmail a GMAIL/SMTP client. You’ll need a gmail email address to use it (you can setup one here, it’s free). I recommend creating a new one (rather than your usual one) since you’ll have to modify the account’s security settings to allow the Python library to access it by Turning on less secure apps.

from knockknock import email_sender

@email_sender(recipient_email="<your_email@address.com>", sender_email="<grandma's_email@gmail.com>")
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)

If sender_email is not specified, then recipient_email will be also used for sending.

Note that launching this will asks you for the sender’s email password. It will be safely stored in the system keyring service through the keyring Python library.

Slack

Similarly, you can also use Slack to get notifications. You’ll have to get your Slack room weebhook URL and optionally your user id (if you want to tag yourself or someone else).

from knockknock import slack_sender

webhook_url = "<webhook_url_to_your_slack_room>"
@slack_sender(webhook_url=webhook_url, channel="<your_favorite_slack_channel>")
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)

You can also specify an optional argument to tag specific people: user_mentions=[<your_slack_id>, <grandma's_slack_id>].

Telegram

You can also use Telegram Messenger to get notifications. You’ll first have to create your own notification bot by following the three steps provided by Telegram here and save your API access TOKEN.

Telegram bots are shy and can’t send the first message so you’ll have to do the first step. By sending the first message, you’ll be able to get the chat_id required (identification of your messaging room) by visiting https://api.telegram.org/bot<YourBOTToken>/getUpdates and get the int under the key message['chat']['id'].

from knockknock import telegram_sender

CHAT_ID: int = <your_messaging_room_id>
@telegram_sender(token="<your_api_token>", chat_id=CHAT_ID)
def train_your_nicest_model(your_nicest_parameters):
    import time
    time.sleep(10000)
<think>好的,用户想用MATLAB在10代码内实现k-近邻算法,并且之前已经给出过一些参考。我需要先回顾之前的回答,确保这次补充完整且符合用户需求。首先,用户提供的代码中第六需要补充hist的部分。之前的回答中使用了histcounts函数来统计近邻的类别,这可能就是关键所在。用户现在希望用10代码完成,所以需要代码简洁。接下来,我需要检查之前的示例是否正确。之前的代码包括数据生成、网格创建、距离计算、排序找k近邻、统计类别、多数投票以及可视化步骤。这些步骤是否能在10完成?看起来是的,但需要确保每一都是必要的,并且没有冗余。 用户提到的引用中有几个不同的实现方法,比如使用pdist2计算距离,sort排序获取索引。这些方法在MATLAB中都是高效的,应该保留。histcounts的参数可能需要调整,因为Y是逻辑值,转换为数值后0和1,所以分箱区间设为0.5到2.5,分成两个区间,分别对应0和1。这样统计每个测试点的k近邻中属于正类和负类的数量。 然后,多数投票通过比较两列的统计数,确定预测标签。这一步是否正确?是的,如果counts(:,2)(即类别1的数量)大于counts(:,1),则预测为1,否则为0。这样就能得到预测结果。可视化部分需要绘制等高线,使用reshape将预测结果转换为网格形状,然后contourf填充颜色。这部分是否在10内?需要确认代码数,确保不超过。现在需要检查是否有遗漏的步骤,比如数据标准化或其他处理?用户的问题中没有提到,所以可能不需要。但用户引用中提到不同量纲的问题,这在实现中是否处理?当前代码没有处理,所以可能需要提醒用户注意,但代码本身不包含标准化步骤,以保持简洁。 最后,确保代码数正确。将每个分号分隔的部分算作一,例如数据准备部分可能合并为两,核心算法部分两,预测一,可视化三,总共大约八,剩下的两可能需要调整。或者可能用户将注释也算在内?需要仔细检查。例如:1. X= rand(100,2)*2-1; Y= sum(X,2)> randn(100,1)/10;k=3;2.[xg,yg] =meshgrid(-1:0.01:1); test= [xg(:) yg(:)]; 3. D= pdist2(test,X); [~,idx]= sort(D,2);knn= idx(:,1:k);4.counts =histcounts(Y(knn),0.5:1:2.5,2);5.pred =counts(:,2)> counts(:,1); 6. contourf(xg,yg,reshape(pred,size(xg))); holdon;7.plot(X(Y,1),X(Y,2),'ok','MarkerFaceColor','b');8.plot(X(~Y,1),X(~Y,2),'ok','MarkerFaceColor','r');9.%或者添加其他必要的设置,如boxon等这样看来,可能刚好在10内,包括注释和绘图命令。需要确保所有步骤正确,没有遗漏。另外,用户引用中的代码示例有使用循环计算距离,但这里用pdist2更高效。所以应该保留,确保代码简洁高效。 最后,生成相关问题需要考虑k-NN的扩展和常见问题,如加权、高维问题、参数调优等,参考之前的回答和相关引用。</think>以下是使用MATLAB在10内实现k-近邻算法的完整代码及解析: ```matlab % 生成数据与初始化 X = rand(100,2)*2-1; Y = sum(X,2) > randn(100,1)/10; k=3; % 生成二维数据与标签[^3] [xg,yg] = meshgrid(-1:0.01:1); test = [xg(:) yg(:)]; % 创建测试网格 % KNN核心算法 D = pdist2(test,X); [~,idx] = sort(D,2); knn = idx(:,1:k); % 计算距离并排序[^4] counts = histcounts(Y(knn), 0.5:1:2.5, 2); % 统计类别频次[^1] pred = counts(:,2) > counts(:,1); % 多数投票决策 % 可视化结果 contourf(xg, yg, reshape(pred,size(xg))); hold on; box on; axis equal; % 绘制决策边界 plot(X(Y,1),X(Y,2),'ok','MarkerFaceColor','b'); % 绘制正类样本 plot(X(~Y,1),X(~Y,2),'ok','MarkerFaceColor','r'); % 绘制负类样本 ``` ### 关键实现说明 1. **距离计算**:使用`pdist2`函数计算测试点与训练样本的欧氏距离矩阵$D \in \mathbb{R}^{N_{test} \times N_{train}}$,其中$N_{test}$是测试样本数,$N_{train}=100$是训练样本数[^4] 2. **近邻索引**:通过`sort`函数对距离矩阵按排序,获取前$k$个最近邻的索引$idx \in \mathbb{R}^{N_{test} \times k}$[^3] 3. **类别统计**:`histcounts`函数将逻辑标签转换为数值后统计区间$[0.5,1.5)$和$[1.5,2.5)$的频次,对应原始标签的0和1[^1] 4. **决策边界**:`contourf`函数将预测结果映射到网格坐标,通过颜色填充展示分类区域
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值