Softmax 练习¶
补充并完成本练习。
本练习类似于SVM练习,你要完成的事情包括:
- 为Softmax分类器实现完全矢量化的损失函数
- 实现其解析梯度(analytic gradient)的完全矢量化表达式
- 用数值梯度检查你的代码
- 使用验证集调整学习率和正则化强度
- 使用SGD优化损失函数
- 可视化最终学习的权重
In [1]:
Copied!
import random
import numpy as np
from daseCV.data_utils import load_CIFAR10
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# for auto-reloading extenrnal modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
import random
import numpy as np
from daseCV.data_utils import load_CIFAR10
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# for auto-reloading extenrnal modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
In [2]:
Copied!
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000, num_dev=500):
"""
Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
it for the linear classifier. These are the same steps as we used for the
SVM, but condensed to a single function.
"""
# Load the raw CIFAR-10 data
cifar10_dir = 'daseCV/datasets/cifar-10-batches-py'
# Cleaning up variables to prevent loading data multiple times (which may cause memory issue)
try:
del X_train, y_train
del X_test, y_test
print('Clear previously loaded data.')
except:
pass
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
# subsample the data
mask = list(range(num_training, num_training + num_validation))
X_val = X_train[mask]
y_val = y_train[mask]
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask]
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
mask = np.random.choice(num_training, num_dev, replace=False)
X_dev = X_train[mask]
y_dev = y_train[mask]
# Preprocessing: reshape the image data into rows
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_val = np.reshape(X_val, (X_val.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
X_dev = np.reshape(X_dev, (X_dev.shape[0], -1))
# Normalize the data: subtract the mean image
mean_image = np.mean(X_train, axis = 0)
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image
X_dev -= mean_image
# add bias dimension and transform into columns
X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))])
X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))])
X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))])
X_dev = np.hstack([X_dev, np.ones((X_dev.shape[0], 1))])
return X_train, y_train, X_val, y_val, X_test, y_test, X_dev, y_dev
# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test, X_dev, y_dev = get_CIFAR10_data()
print('Train data shape: ', X_train.shape)
print('Train labels shape: ', y_train.shape)
print('Validation data shape: ', X_val.shape)
print('Validation labels shape: ', y_val.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)
print('dev data shape: ', X_dev.shape)
print('dev labels shape: ', y_dev.shape)
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000, num_dev=500):
"""
Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
it for the linear classifier. These are the same steps as we used for the
SVM, but condensed to a single function.
"""
# Load the raw CIFAR-10 data
cifar10_dir = 'daseCV/datasets/cifar-10-batches-py'
# Cleaning up variables to prevent loading data multiple times (which may cause memory issue)
try:
del X_train, y_train
del X_test, y_test
print('Clear previously loaded data.')
except:
pass
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
# subsample the data
mask = list(range(num_training, num_training + num_validation))
X_val = X_train[mask]
y_val = y_train[mask]
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask]
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
mask = np.random.choice(num_training, num_dev, replace=False)
X_dev = X_train[mask]
y_dev = y_train[mask]
# Preprocessing: reshape the image data into rows
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_val = np.reshape(X_val, (X_val.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
X_dev = np.reshape(X_dev, (X_dev.shape[0], -1))
# Normalize the data: subtract the mean image
mean_image = np.mean(X_train, axis = 0)
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image
X_dev -= mean_image
# add bias dimension and transform into columns
X_train = np.hstack([X_train, np.ones((X_train.shape[0], 1))])
X_val = np.hstack([X_val, np.ones((X_val.shape[0], 1))])
X_test = np.hstack([X_test, np.ones((X_test.shape[0], 1))])
X_dev = np.hstack([X_dev, np.ones((X_dev.shape[0], 1))])
return X_train, y_train, X_val, y_val, X_test, y_test, X_dev, y_dev
# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test, X_dev, y_dev = get_CIFAR10_data()
print('Train data shape: ', X_train.shape)
print('Train labels shape: ', y_train.shape)
print('Validation data shape: ', X_val.shape)
print('Validation labels shape: ', y_val.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)
print('dev data shape: ', X_dev.shape)
print('dev labels shape: ', y_dev.shape)
Train data shape: (49000, 3073) Train labels shape: (49000,) Validation data shape: (1000, 3073) Validation labels shape: (1000,) Test data shape: (1000, 3073) Test labels shape: (1000,) dev data shape: (500, 3073) dev labels shape: (500,)
Softmax 分类器¶
请在daseCV/classifiers/softmax.py中完成本节的代码。
In [3]:
Copied!
# 首先使用嵌套循环实现简单的softmax损失函数。
# 打开文件 daseCV/classifiers/softmax.py 并补充完成
# softmax_loss_naive 函数.
from daseCV.classifiers.softmax import softmax_loss_naive
import time
# 生成一个随机的softmax权重矩阵,并使用它来计算损失。
W = np.random.randn(3073, 10) * 0.0001
loss, grad = softmax_loss_naive(W, X_dev, y_dev, 0.0)
# As a rough sanity check, our loss should be something close to -log(0.1).
print('loss: %f' % loss)
print('sanity check: %f' % (-np.log(0.1)))
# 首先使用嵌套循环实现简单的softmax损失函数。
# 打开文件 daseCV/classifiers/softmax.py 并补充完成
# softmax_loss_naive 函数.
from daseCV.classifiers.softmax import softmax_loss_naive
import time
# 生成一个随机的softmax权重矩阵,并使用它来计算损失。
W = np.random.randn(3073, 10) * 0.0001
loss, grad = softmax_loss_naive(W, X_dev, y_dev, 0.0)
# As a rough sanity check, our loss should be something close to -log(0.1).
print('loss: %f' % loss)
print('sanity check: %f' % (-np.log(0.1)))
loss: 2.311035 sanity check: 2.302585
问题 1
为什么我们期望损失接近-log(0.1)?简要说明。
$\color{blue}{\textit 答:}$ 在这里写上你的答案
由于随机初始化了权值, 可以假设输出具有标准且独立的10维Dirichlet分布, 任选任一维, 其对数的期望是 $-\log(0.1)$, 根据大数定律, 样本均值随实验次数依概率收敛到 $-\log(0.1)$ .
In [4]:
Copied!
# 完成softmax_loss_naive,并实现使用嵌套循环的梯度的版本(naive)。
loss, grad = softmax_loss_naive(W, X_dev, y_dev, 0.0)
# 就像SVM那样,请使用数值梯度检查作为调试工具。
# 数值梯度应接近分析梯度。
from daseCV.gradient_check import grad_check_sparse
f = lambda w: softmax_loss_naive(w, X_dev, y_dev, 0.0)[0]
grad_numerical = grad_check_sparse(f, W, grad, 10)
# 与SVM情况类似,使用正则化进行另一个梯度检查
loss, grad = softmax_loss_naive(W, X_dev, y_dev, 5e1)
f = lambda w: softmax_loss_naive(w, X_dev, y_dev, 5e1)[0]
grad_numerical = grad_check_sparse(f, W, grad, 10)
# 完成softmax_loss_naive,并实现使用嵌套循环的梯度的版本(naive)。
loss, grad = softmax_loss_naive(W, X_dev, y_dev, 0.0)
# 就像SVM那样,请使用数值梯度检查作为调试工具。
# 数值梯度应接近分析梯度。
from daseCV.gradient_check import grad_check_sparse
f = lambda w: softmax_loss_naive(w, X_dev, y_dev, 0.0)[0]
grad_numerical = grad_check_sparse(f, W, grad, 10)
# 与SVM情况类似,使用正则化进行另一个梯度检查
loss, grad = softmax_loss_naive(W, X_dev, y_dev, 5e1)
f = lambda w: softmax_loss_naive(w, X_dev, y_dev, 5e1)[0]
grad_numerical = grad_check_sparse(f, W, grad, 10)
numerical: 0.624739 analytic: 0.624739, relative error: 4.684709e-09 numerical: -0.037933 analytic: -0.037933, relative error: 4.704150e-07 numerical: -0.798497 analytic: -0.798497, relative error: 1.504982e-08 numerical: -0.278850 analytic: -0.278850, relative error: 1.226482e-08 numerical: 1.383547 analytic: 1.383547, relative error: 3.360547e-08 numerical: -1.933946 analytic: -1.933946, relative error: 4.429242e-09 numerical: 1.021953 analytic: 1.021953, relative error: 9.724111e-09 numerical: 0.898275 analytic: 0.898275, relative error: 2.082841e-08 numerical: 1.806236 analytic: 1.806236, relative error: 1.945668e-08 numerical: 2.191082 analytic: 2.191082, relative error: 2.079263e-08 numerical: -0.303095 analytic: -0.303095, relative error: 1.876084e-07 numerical: -0.951560 analytic: -0.951560, relative error: 3.044580e-08 numerical: 1.346033 analytic: 1.346033, relative error: 1.048233e-09 numerical: -1.161873 analytic: -1.161873, relative error: 2.489886e-08 numerical: 0.036525 analytic: 0.036525, relative error: 2.098502e-06 numerical: -2.021091 analytic: -2.021091, relative error: 3.352103e-08 numerical: 0.045599 analytic: 0.045599, relative error: 7.999047e-08 numerical: 0.666989 analytic: 0.666989, relative error: 1.883165e-08 numerical: -0.449728 analytic: -0.449728, relative error: 1.829864e-07 numerical: -2.169907 analytic: -2.169908, relative error: 4.836657e-08
In [5]:
Copied!
# 现在,我们有了softmax损失函数及其梯度的简单实现,
# 接下来要在 softmax_loss_vectorized 中完成一个向量化版本.
# 这两个版本应计算出相同的结果,但矢量化版本应更快。
tic = time.time()
loss_naive, grad_naive = softmax_loss_naive(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('naive loss: %e computed in %fs' % (loss_naive, toc - tic))
from daseCV.classifiers.softmax import softmax_loss_vectorized
tic = time.time()
loss_vectorized, grad_vectorized = softmax_loss_vectorized(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('vectorized loss: %e computed in %fs' % (loss_vectorized, toc - tic))
# 正如前面在SVM练习中所做的一样,我们使用Frobenius范数比较两个版本梯度。
grad_difference = np.linalg.norm(grad_naive - grad_vectorized, ord='fro')
print('Loss difference: %f' % np.abs(loss_naive - loss_vectorized))
print('Gradient difference: %f' % grad_difference)
# 现在,我们有了softmax损失函数及其梯度的简单实现,
# 接下来要在 softmax_loss_vectorized 中完成一个向量化版本.
# 这两个版本应计算出相同的结果,但矢量化版本应更快。
tic = time.time()
loss_naive, grad_naive = softmax_loss_naive(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('naive loss: %e computed in %fs' % (loss_naive, toc - tic))
from daseCV.classifiers.softmax import softmax_loss_vectorized
tic = time.time()
loss_vectorized, grad_vectorized = softmax_loss_vectorized(W, X_dev, y_dev, 0.000005)
toc = time.time()
print('vectorized loss: %e computed in %fs' % (loss_vectorized, toc - tic))
# 正如前面在SVM练习中所做的一样,我们使用Frobenius范数比较两个版本梯度。
grad_difference = np.linalg.norm(grad_naive - grad_vectorized, ord='fro')
print('Loss difference: %f' % np.abs(loss_naive - loss_vectorized))
print('Gradient difference: %f' % grad_difference)
naive loss: 2.311035e+00 computed in 0.002555s vectorized loss: 2.311035e+00 computed in 0.002247s Loss difference: 0.000000 Gradient difference: 0.000000
In [6]:
Copied!
# 使用验证集调整超参数(正则化强度和学习率)。您应该尝试不同的学习率和正则化强度范围;
# 如果您小心的话,您应该能够在验证集上获得超过0.35的精度。
from daseCV.classifiers import Softmax
results = {}
best_val = -1
best_softmax = None
learning_rates = np.random.randint(1,100,5) * 1e-8
regularization_strengths = np.random.randint(1,100,5) * 1e3
################################################################################
# 需要完成的事:
# 对验证集设置学习率和正则化强度。
# 这与之前SVM中做的类似;
# 保存训练效果最好的softmax分类器到best_softmax中。
################################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
for lr in learning_rates:
for reg in regularization_strengths:
cur_softmax = Softmax()
loss_history = cur_softmax.train(X_train, y_train, learning_rate=lr, reg=reg, num_iters=1000)
train_pred = cur_softmax.predict(X_train)
val_pred = cur_softmax.predict(X_val)
train_accuracy = np.sum(train_pred==y_train) / len(train_pred)
val_accuracy = np.sum(val_pred==y_val) / len(val_pred)
results[(lr, reg)] = (train_accuracy, val_accuracy)
if val_accuracy > best_val:
best_val = val_accuracy
best_softmax = cur_softmax
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
# Print out results.
for lr, reg in sorted(results):
train_accuracy, val_accuracy = results[(lr, reg)]
print('lr %e reg %e train accuracy: %f val accuracy: %f' % (
lr, reg, train_accuracy, val_accuracy))
print('best validation accuracy achieved during cross-validation: %f' % best_val)
# 使用验证集调整超参数(正则化强度和学习率)。您应该尝试不同的学习率和正则化强度范围;
# 如果您小心的话,您应该能够在验证集上获得超过0.35的精度。
from daseCV.classifiers import Softmax
results = {}
best_val = -1
best_softmax = None
learning_rates = np.random.randint(1,100,5) * 1e-8
regularization_strengths = np.random.randint(1,100,5) * 1e3
################################################################################
# 需要完成的事:
# 对验证集设置学习率和正则化强度。
# 这与之前SVM中做的类似;
# 保存训练效果最好的softmax分类器到best_softmax中。
################################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
for lr in learning_rates:
for reg in regularization_strengths:
cur_softmax = Softmax()
loss_history = cur_softmax.train(X_train, y_train, learning_rate=lr, reg=reg, num_iters=1000)
train_pred = cur_softmax.predict(X_train)
val_pred = cur_softmax.predict(X_val)
train_accuracy = np.sum(train_pred==y_train) / len(train_pred)
val_accuracy = np.sum(val_pred==y_val) / len(val_pred)
results[(lr, reg)] = (train_accuracy, val_accuracy)
if val_accuracy > best_val:
best_val = val_accuracy
best_softmax = cur_softmax
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
# Print out results.
for lr, reg in sorted(results):
train_accuracy, val_accuracy = results[(lr, reg)]
print('lr %e reg %e train accuracy: %f val accuracy: %f' % (
lr, reg, train_accuracy, val_accuracy))
print('best validation accuracy achieved during cross-validation: %f' % best_val)
lr 2.000000e-08 reg 9.000000e+03 train accuracy: 0.177449 val accuracy: 0.192000 lr 2.000000e-08 reg 2.100000e+04 train accuracy: 0.199000 val accuracy: 0.221000 lr 2.000000e-08 reg 3.600000e+04 train accuracy: 0.224592 val accuracy: 0.223000 lr 2.000000e-08 reg 8.600000e+04 train accuracy: 0.281980 val accuracy: 0.284000 lr 2.000000e-08 reg 9.800000e+04 train accuracy: 0.287469 val accuracy: 0.314000 lr 2.700000e-07 reg 9.000000e+03 train accuracy: 0.358918 val accuracy: 0.364000 lr 2.700000e-07 reg 2.100000e+04 train accuracy: 0.336122 val accuracy: 0.342000 lr 2.700000e-07 reg 3.600000e+04 train accuracy: 0.305184 val accuracy: 0.320000 lr 2.700000e-07 reg 8.600000e+04 train accuracy: 0.297939 val accuracy: 0.314000 lr 2.700000e-07 reg 9.800000e+04 train accuracy: 0.271469 val accuracy: 0.281000 lr 4.100000e-07 reg 9.000000e+03 train accuracy: 0.353429 val accuracy: 0.373000 lr 4.100000e-07 reg 2.100000e+04 train accuracy: 0.324408 val accuracy: 0.338000 lr 4.100000e-07 reg 3.600000e+04 train accuracy: 0.307041 val accuracy: 0.325000 lr 4.100000e-07 reg 8.600000e+04 train accuracy: 0.289306 val accuracy: 0.303000 lr 4.100000e-07 reg 9.800000e+04 train accuracy: 0.302408 val accuracy: 0.314000 lr 8.400000e-07 reg 9.000000e+03 train accuracy: 0.350224 val accuracy: 0.360000 lr 8.400000e-07 reg 2.100000e+04 train accuracy: 0.339224 val accuracy: 0.337000 lr 8.400000e-07 reg 3.600000e+04 train accuracy: 0.308286 val accuracy: 0.316000 lr 8.400000e-07 reg 8.600000e+04 train accuracy: 0.299143 val accuracy: 0.304000 lr 8.400000e-07 reg 9.800000e+04 train accuracy: 0.280306 val accuracy: 0.293000 lr 9.700000e-07 reg 9.000000e+03 train accuracy: 0.353265 val accuracy: 0.367000 lr 9.700000e-07 reg 2.100000e+04 train accuracy: 0.316469 val accuracy: 0.325000 lr 9.700000e-07 reg 3.600000e+04 train accuracy: 0.305102 val accuracy: 0.317000 lr 9.700000e-07 reg 8.600000e+04 train accuracy: 0.272265 val accuracy: 0.283000 lr 9.700000e-07 reg 9.800000e+04 train accuracy: 0.271531 val accuracy: 0.274000 best validation accuracy achieved during cross-validation: 0.373000
In [7]:
Copied!
# 在测试集上评估
# 在测试集上评估最好的softmax
y_test_pred = best_softmax.predict(X_test)
test_accuracy = np.mean(y_test == y_test_pred)
print('softmax on raw pixels final test set accuracy: %f' % (test_accuracy, ))
# 在测试集上评估
# 在测试集上评估最好的softmax
y_test_pred = best_softmax.predict(X_test)
test_accuracy = np.mean(y_test == y_test_pred)
print('softmax on raw pixels final test set accuracy: %f' % (test_accuracy, ))
softmax on raw pixels final test set accuracy: 0.362000
问题 2 - 对或错
假设总训练损失定义为所有训练样本中每个数据点损失的总和。可能会有新的数据点添加到训练集中,同时SVM损失保持不变,但是对于Softmax分类器的损失而言,情况并非如此。
$\color{blue}{\textit 你的回答:}$
正确, SVM损失可能不变, 但Softmax损失一定变化.
$\color{blue}{你的解释:}$
当该点被正确分类, 并处于Margin外时, SVM损失为零, 故总和不变. 对Softmax来说, 对任一 $i$, $-log(e^{x_i}/Z)$ 的值总是大于零(其中 Z 表示归一化系数), 尽管它可能很小.
In [8]:
Copied!
# 可视化每个类别的学习到的权重
w = best_softmax.W[:-1,:] # strip out the bias
w = w.reshape(32, 32, 3, 10)
w_min, w_max = np.min(w), np.max(w)
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
for i in range(10):
plt.subplot(2, 5, i + 1)
# Rescale the weights to be between 0 and 255
wimg = 255.0 * (w[:, :, :, i].squeeze() - w_min) / (w_max - w_min)
plt.imshow(wimg.astype('uint8'))
plt.axis('off')
plt.title(classes[i])
# 可视化每个类别的学习到的权重
w = best_softmax.W[:-1,:] # strip out the bias
w = w.reshape(32, 32, 3, 10)
w_min, w_max = np.min(w), np.max(w)
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
for i in range(10):
plt.subplot(2, 5, i + 1)
# Rescale the weights to be between 0 and 255
wimg = 255.0 * (w[:, :, :, i].squeeze() - w_min) / (w_max - w_min)
plt.imshow(wimg.astype('uint8'))
plt.axis('off')
plt.title(classes[i])
In [9]:
Copied!
# leaderboard的测试数据
X = np.load("./input/X_3073.npy")
################################################################################
# 需要完成的事情:
# 找到更合适的softmax
# 提示:如果你不想花时间,你也可以直接使用上面已经训练好的best_softmax。
################################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
softmax_leaderboard = best_softmax
preds = softmax_leaderboard.predict(X)
# leaderboard的测试数据
X = np.load("./input/X_3073.npy")
################################################################################
# 需要完成的事情:
# 找到更合适的softmax
# 提示:如果你不想花时间,你也可以直接使用上面已经训练好的best_softmax。
################################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
softmax_leaderboard = best_softmax
preds = softmax_leaderboard.predict(X)
提醒:运行完下面代码之后,点击下面的submit,然后去leaderboard上查看你的成绩。本模型对应的成绩在phase3的leaderboard中。
In [10]:
Copied!
import os
#输出格式
def output_file(preds, phase_id=3):
path=os.getcwd()
if not os.path.exists(path + '/output/phase_{}'.format(phase_id)):
os.mkdir(path + '/output/phase_{}'.format(phase_id))
path=path + '/output/phase_{}/prediction.npy'.format(phase_id)
np.save(path,preds)
def zip_fun(phase_id=3):
path=os.getcwd()
output_path = path + '/output'
files = os.listdir(output_path)
for _file in files:
if _file.find('zip') != -1:
os.remove(output_path + '/' + _file)
newpath=path+'/output/phase_{}'.format(phase_id)
os.chdir(newpath)
cmd = 'zip ../prediction_phase_{}.zip prediction.npy'.format(phase_id)
os.system(cmd)
os.chdir(path)
output_file(preds)
zip_fun()
import os
#输出格式
def output_file(preds, phase_id=3):
path=os.getcwd()
if not os.path.exists(path + '/output/phase_{}'.format(phase_id)):
os.mkdir(path + '/output/phase_{}'.format(phase_id))
path=path + '/output/phase_{}/prediction.npy'.format(phase_id)
np.save(path,preds)
def zip_fun(phase_id=3):
path=os.getcwd()
output_path = path + '/output'
files = os.listdir(output_path)
for _file in files:
if _file.find('zip') != -1:
os.remove(output_path + '/' + _file)
newpath=path+'/output/phase_{}'.format(phase_id)
os.chdir(newpath)
cmd = 'zip ../prediction_phase_{}.zip prediction.npy'.format(phase_id)
os.system(cmd)
os.chdir(path)
output_file(preds)
zip_fun()
In [ ]:
Copied!