pytest clear test memory after yield - memory

I wrapped all of my pytests tests with the following fixture:
import pytest
import nvidia_smi
def gpu_memory_used():
nvidia_smi.nvmlInit()
device_count = nvidia_smi.nvmlDeviceGetCount()
assert device_count == 1, 'Should be 1 GPU'
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
used_memory = info.used
nvidia_smi.nvmlShutdown()
return used_memory
#pytest.fixture(autouse=True)
def check_gpu_memory():
memory_used_before_test = gpu_memory_used()
yield
memory_used_after_test = gpu_memory_used()
assert memory_used_after_test == memory_used_before_test
Now I have the following dummy test function that runs small training:
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import shutil
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=-1)
def training(work_dir, epoch, network, optimizer, train_loader, log_interval, train_counter, train_losses):
network.train()
for batch_idx, (data, target) in enumerate(train_loader):
optimizer.zero_grad()
output = network(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
train_losses.append(loss.item())
train_counter.append(
(batch_idx*64) + ((epoch-1)*len(train_loader.dataset)))
torch.save(network.state_dict(), f'{work_dir}/model.pth')
torch.save(optimizer.state_dict(), f'{work_dir}/optimizer.pth')
def validating(network, test_loader, test_losses):
network.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
output = network(data)
test_loss += F.nll_loss(output, target, size_average=False).item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).sum()
test_loss /= len(test_loader.dataset)
test_losses.append(test_loss)
print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
def test_david_torch(tmp_path):
n_epochs = 3
batch_size_train = 128
batch_size_test = 1000
learning_rate = 0.01
momentum = 0.5
log_interval = 10
network = Net()
optimizer = optim.SGD(network.parameters(), lr=learning_rate,
momentum=momentum)
random_seed = 1
# torch.backends.cudnn.enabled = False
# torch.manual_seed(random_seed)
train_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST(f'{str(tmp_path)}/files/', train=True, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1307,), (0.3081,))
])),
batch_size=batch_size_train, shuffle=True)
test_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST(f'{str(tmp_path)}/files/', train=False, download=True,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1307,), (0.3081,))
])),
batch_size=batch_size_test, shuffle=True)
train_losses = []
train_counter = []
test_losses = []
test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)]
validating(network, test_loader, test_losses)
for epoch in range(1, n_epochs + 1):
training(str(tmp_path), epoch, network, optimizer, train_loader, log_interval, train_counter, train_losses)
validating(network, test_loader, test_losses)
shutil.rmtree(str(tmp_path))
Before the test is called I see in nvidia-smi that no memory is used, the problem is that after the yield memory is still being used.
Is there a way to avoid such behaviour?
I tried clearing the cuda cache etc, but nothing really free the memory.
Before Test:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.82.01 Driver Version: 470.82.01 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 28C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
After Test (for my real case the memory usage is much larger):
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.82.01 Driver Version: 470.82.01 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 28C P8 9W / 70W | 3MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+

Related

Need help about my Connect 4 Deep Reinforcement Learning Agent

Here is my code
My env setting
state: np.array, 1 * 6 * 7 (1 input channel for CNN, 6 * 7 the size of the board), 0 = nothing, 1 = agent1's token, -1 =
agent2's token
reward function: 0 if draw or nothing happen, 1 if win, -1 if lose
I know that Connect 4 is a solved game, but I want to try making two value based method agents to learn the game from scratch. I tried ANN with flatten the state as input, and output 7 value(argmax to get action), but this method make the agent converge to a stupid strategy.
Example
_ | _ | _ | _ | _ | _ | _
_ | _ | _ | _ | _ | _ | _
_ | X | _ | _ | _ | _ | _
_ | X | _ | O | _ | _ | _
_ | X | _ | O | _ | _ | _
_ | X | _ | O | _ | _ | _
Then the agent who go first always win with this "strategy".
Then, I tried CNN and n step learning but it still converge to the "strategy", here is the CNN architecture
class Network(nn.Module):
def __init__(self, output_dim: int, learning_rate: float) -> None:
super(Network, self).__init__()
self.feature_layers = nn.Sequential(
nn.Conv2d(1, 64, 4),
nn.ReLU(),
nn.Conv2d(64, 32, 2),
nn.ReLU(),
nn.Conv2d(32, 16, 2),
nn.Flatten(),
)
self.advantage_layers = nn.Sequential(
nn.Linear(32, 16),
nn.ReLU(),
nn.Linear(16, output_dim)
)
self.value_layers = nn.Sequential(
nn.Linear(32, 8),
nn.ReLU(),
nn.Linear(8, 1)
)
self.optimizer = optim.RMSprop(self.parameters(), lr = learning_rate)
self.loss = nn.MSELoss()
def forward(self, x: torch.Tensor) -> torch.Tensor:
feature = self.feature_layers(x)
advantage = self.advantage_layers(feature)
value = self.value_layers(feature)
return value + advantage - advantage.mean(dim = -1, keepdim = True)
Is there anything I did wrong
(I know that MCTS is very good for this case, but I am still learning value based method)

How can take n samples/events from all predicted clusters and return in form of dataframe in PySpark?

I'm following this tutorial and try to pick/select top n events, let's say 10 events/rows after assigning the predicted clusters to main df and merge them and report it in the form of the spark data frame.
Let's say we I have main dataframe df1 contains the 3 features as below:
+-----+------------+----------+----------+
| id| x| y| z|
+-----+------------+----------+----------+
| row0| -6.0776997|-2.9096103|-1.5181729|
| row1| -1.0122601| 7.322841|-5.4424076|
| row2| -8.297007| 6.3228936| 1.1672047|
| row3| -3.5071216| 4.784812|-5.4449472|
| row4| -5.122823|-3.3220499|-0.5069805|
| row5| -2.4764006| 8.255791| 4.409478|
| row6| 7.3153954| -5.079449| -7.291215|
| row7| -2.0167463| 9.303454| 7.095179|
| row8| -0.2338185| -4.892681| 2.1228876|
| row9| 6.565442| -6.855994|-6.7983212|
|row10| -5.6902847|-6.4827404|-0.9246967|
|row11|-0.017986143| 2.7632365| -8.814824|
|row12| -6.9042625|-6.1491723|-3.5354295|
|row13| -10.389865| 9.537853| 0.674591|
|row14| 3.9688683|-6.0467844| -5.462389|
|row15| -7.337052|-3.7689247| -5.261122|
|row16| -8.991589| 8.738728| 3.864116|
|row17| -0.18098584| 5.482743| -4.900118|
|row18| 3.3193955|-6.3573766| -6.978025|
|row19| -2.0266335|-3.4171724|0.48218703|
+-----+------------+----------+----------+
now I have information out of the clustering algorithm in the form of the datafarame df2 as below:
print("==========================Short report==================================== ")
n_clusters = model.summary.k
#n_clusters
print("Number of predicted clusters: " + str(n_clusters))
cluster_Sizes = model.summary.clusterSizes
#cluster_Sizes
col = ['size']
df2 = pd.DataFrame(cluster_Sizes, columns=col).sort_values(by=['size'], ascending=True) #sorting
cluster_Sizes = df2["size"].unique()
print("Size of predicted clusters: " + str(cluster_Sizes))
clusterSizes
#==========================Short report====================================
#Number of predicted clusters: 10
#Size of predicted clusters: [ 486 496 504 529 985 998 999 1003 2000]
+-----+----------+
| |prediction|
+-----+----------+
| 2| 486|
| 6| 496|
| 0| 504|
| 8| 529|
| 5| 985|
| 9| 998|
| 7| 999|
| 3| 1003|
| 1| 2000|
| 4| 2000|
+-----+----------+
So here, the index column is predicted cluster labels. I could assign the predicted cluster labels into the main dataframe but not cluster size as below:
+-----+----------+------------+----------+----------+
| id|prediction| x| y| z|
+-----+----------+------------+----------+----------+
| row0| 9| -6.0776997|-2.9096103|-1.5181729|
| row1| 4| -1.0122601| 7.322841|-5.4424076|
| row2| 1| -8.297007| 6.3228936| 1.1672047|
| row3| 4| -3.5071216| 4.784812|-5.4449472|
| row4| 3| -5.122823|-3.3220499|-0.5069805|
| row5| 1| -2.4764006| 8.255791| 4.409478|
| row6| 5| 7.3153954| -5.079449| -7.291215|
| row7| 1| -2.0167463| 9.303454| 7.095179|
| row8| 7| -0.2338185| -4.892681| 2.1228876|
| row9| 5| 6.565442| -6.855994|-6.7983212|
|row10| 3| -5.6902847|-6.4827404|-0.9246967|
|row11| 4|-0.017986143| 2.7632365| -8.814824|
|row12| 9| -6.9042625|-6.1491723|-3.5354295|
|row13| 1| -10.389865| 9.537853| 0.674591|
|row14| 2| 3.9688683|-6.0467844| -5.462389|
|row15| 9| -7.337052|-3.7689247| -5.261122|
|row16| 1| -8.991589| 8.738728| 3.864116|
|row17| 4| -0.18098584| 5.482743| -4.900118|
|row18| 2| 3.3193955|-6.3573766| -6.978025|
|row19| 7| -2.0266335|-3.4171724|0.48218703|
+-----+----------+------------+----------+----------+
Now, want to include\report top n rows of each cluster in the form of the dataframe via the following function. What I have tried till now is using (multi-)conditional filtering:
print("==========================Short report==================================== ")
n_clusters = model.summary.k
#n_clusters
print("Number of predicted clusters: " + str(n_clusters))
cluster_Sizes = model.summary.clusterSizes
#cluster_Sizes
col = ['size']
clusterSizes = pd.DataFrame(cluster_Sizes, columns=col).sort_values(by=['size'], ascending=True) #sorting
cluster_Sizes = clusterSizes["size"].unique()
print("Size of predicted clusters: " + str(cluster_Sizes))
clusterSizes
from pyspark.sql.functions import max, min
def cls_report(df):
x1=df.select([min("x")]) # will return max value of each column
x2=df.select([max("y")])
x3=df.select([max("z")])
return x1,x2,x3
#pick top out clusters with minimum instances
km_1st_cls = clusterSizes.values[0]
km_2nd_cls = clusterSizes.values[1]
km_3rd_cls = clusterSizes.values[2]
print(km_1st_cls)
print(km_2nd_cls)
print(km_3rd_cls)
#F1 = cls_report(pddf_pred.filter(f"prediction == {km_1st_cls}"))[0]
F1 = cls_report(pddf_pred.filter(f"prediction == {km_1st_cls}"))[0].select("min(x)").rdd.flatMap(list).collect()[0]
F2 = cls_report(pddf_pred.filter(f"prediction == {km_2nd_cls}"))[0].select("min(x)").rdd.flatMap(list).collect()[0]
F3 = cls_report(pddf_pred.filter(f"prediction == {km_3rd_cls}"))[0].select("min(x)").rdd.flatMap(list).collect()[0]
L1 = cls_report(pddf_pred.filter(f"prediction == {km_1st_cls}"))[1].select("max(y)").rdd.flatMap(list).collect()[0]
L2 = cls_report(pddf_pred.filter(f"prediction == {km_2nd_cls}"))[1].select("max(y)").rdd.flatMap(list).collect()[0]
L3 = cls_report(pddf_pred.filter(f"prediction == {km_3rd_cls}"))[1].select("max(y)").rdd.flatMap(list).collect()[0]
T1 = cls_report(pddf_pred.filter(f"prediction == {km_1st_cls}"))[2].select("max(z)").rdd.flatMap(list).collect()[0]
T2 = cls_report(pddf_pred.filter(f"prediction == {km_2nd_cls}"))[2].select("max(z)").rdd.flatMap(list).collect()[0]
T3 = cls_report(pddf_pred.filter(f"prediction == {km_3rd_cls}"))[2].select("max(z)").rdd.flatMap(list).collect()[0]
print(F1)
print(F2)
print(F3)
print(L1)
print(L2)
print(L3)
print(T1)
print(T2)
print(T3)
df_anomaly_1st_cls = pddf_pred.filter(f"(prediction == {km_1st_cls})") \
.filter(f"y == {L1}") \
.filter(f"z == {T1}") \
.filter(f"x == {F1}")
display(df_anomaly_1st_cls)
I know that in KM algorithm in SciKit-learn we could use based on this post:
clusters=KMeans(n_clusters=5)
df[clusters.labels_==0]
But we don't have access to such labels_ in spark for the quick hack for this task.
Is there any elegant way (maybe define the function) to call it so that we can reflect the results of any clustering algorithm on the main dataframe for better reasoning?
Note: I'm not interested in hacking it by converting spark frame into Pandas datafarme using .toPandas()
update: I might need a function to automate filtering based on multi-conditions gets input dataframe and number of top cluster based on their instances and number of events/rows, in output in returns filtered/selected stacked results in which mock of the expected dataframe is following:
def filtering(df, top_out_cluster=2, top_events/rows=2):
# +-----+----------+------------+----------+----------+
# | id|prediction| x| y| z|
# +-----+----------+------------+----------+----------+
#1st top out cluster | row4| 3| -5.122823|-3.3220499|-0.5069805|
#conditions F1, L1, T1 |row10| 3| -5.6902847|-6.4827404|-0.9246967|
#2nd top out cluster | row8| 7| -0.2338185| -4.892681| 2.1228876|
#conditions F1, L1, T1 |row19| 7| -2.0266335|-3.4171724|0.48218703|
#3rd top out cluster |row18| 2| 3.3193955|-6.3573766| -6.978025|
#conditions F1, L1, T1 |row14| 2| 3.9688683|-6.0467844| -5.462389|
#1st top out cluster | row6| 5| 7.3153954| -5.079449| -7.291215|
#conditions F2, L2, T2 | row9| 5| 6.565442| -6.855994|-6.7983212|
#2nd top out cluster |row12| 9| -6.9042625|-6.1491723|-3.5354295|
#conditions F2, L2, T2 | row0| 9| -6.0776997|-2.9096103|-1.5181729|
#1st top out cluster | row1| 4| -1.0122601| 7.322841|-5.4424076|
#conditions F3, L3, T3 |row11| 4|-0.017986143| 2.7632365| -8.814824|
#2nd top out cluster |row13| 1| -10.389865| 9.537853| 0.674591|
#conditions F3, L3, T3 | row5| 1| -2.4764006| 8.255791| 4.409478|
# +-----+----------+------------+----------+----------+
as per your requirements discussed over colab, following is the code.
from pyspark.sql.window import Window
from pyspark.sql import functions as f
from pyspark.sql.functions import row_number
winspec = Window.partitionBy("prediction").orderBy("prediction")
def get_top_n_clusters(model, top_out_cluster: int):
n_clusters = model.summary.k
cluster_size = model.summary.clusterSizes
if top_out_cluster > n_clusters:
raise ValueError(f"n value cannot be greater than cluster_size")
col = ['size']
cluster_size = pd.DataFrame(cluster_size, columns=col).sort_values(by=['size'], ascending=True) #sorting
return list(cluster_size.head(top_out_cluster).index)
def filtering(df, labels: list, top_records: int):
winspec = Window.partitionBy("prediction").orderBy("prediction")
return (
df.filter(f.col("prediction").isin(labels))
.withColumn("rowNum", f.row_number().over(winspec))
.withColumn("minX", min(f.col("x")).over(winspec))
.withColumn("maxY", max(f.col("y")).over(winspec))
.withColumn("maxZ", max(f.col("z")).over(winspec))
.filter(f.col("rowNum")<=top_records)
.selectExpr("id", "prediction", "minX as x", "maxY as y", "maxZ as z")
)
cluster_labels = get_top_n_clusters(model, top_out_cluster=4)
fdf = filtering(df_pred, labels=cluster_labels, top_records=1)
fdf.show()
+-----+----------+----------+----------+-----------+
| id|prediction| x| y| z|
+-----+----------+----------+----------+-----------+
|row15| 7|-10.505684|-1.6820424|-0.32242402|
| row4| 9| -9.426199| 0.5639291| 3.5664654|
| row0| 2| -8.317323|-1.3278837|-0.33906546|
|row14| 4|-0.9338185|-3.6411285| -3.7280529|
+-----+----------+----------+----------+-----------+

How to know scikit-learn confusion matrix's label order and change it

There is a multi-classification problem with 27 classes.
y_predict=[0 0 0 20 26 21 21 26 ....]
y_true=[1 10 10 20 26 21 18 26 ...]
A list named "answer_vocabulary" stored the corresponding 27 words to each index.
answer_vocabulary=[0 1 10 11 2 3 agriculture commercial east living north .....]
cm = confusion_matrix(y_true=y_true, y_pred=y_predict)
I'm confused about the order of the confusion matrix. It is in an ascending index order? And if I want to reorder the confusion matrix with a label sequence=[0 1 2 3 10 11 agriculture commercial living east north ...], how can I implement it?
Here is a function I have tried to plot confusion matrix.
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
The confusion matrices from sklearn don't store information about how the matrix was created (class ordering, and normalization): this means you must use the confusion matrix as soon as you create it or the information will be lost.
By default, sklearn.metrics.confusion_matrix(y_true,y_pred) create the matrix in the order the classes appear in y_true.
If you pass this data to sklearn.metrix.confusion_matrix:
+--------+--------+
| y_true | y_pred |
+--------+--------+
| A | B |
| C | C |
| D | B |
| B | A |
+--------+--------+
Scikit-leart will create this confusion matrix (zeros omited):
+-----------+---+---+---+---+
| true\pred | A | C | D | B |
+-----------+---+---+---+---+
| A | | | | 1 |
| C | | 1 | | |
| D | | | | 1 |
| B | 1 | | | |
+-----------+---+---+---+---+
And it will return this numpy matrix to you:
+---+---+---+---+
| 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 |
+---+---+---+---+
If you want to select classes, or reorder them you can pass the 'labels' argument to confusion_matrix().
For reordering:
labels = ['D','C','B','A']
mat = confusion_matrix(true_y,pred_y, labels=labels)
Or, if you just want to focus on some labels (useful if you have a lot of labels):
labels = ['A','D']
mat = confusion_matrix(true_y,pred_y, labels=labels)
Also,take a look at sklearn.metrics.plot_confusion_matrix. It works very well for small (<100) classes.
If you have >100 classes it will take a white to plot the matrix.
The order of the columns/rows in the resulting confusion matrix is the same as returned by sklearn.utils.unique_labels(), which extracts "an ordered array of unique labels". In the source code of confusion_matrix() (main, git-hash 7e197fd), the lines of interest read as follows
if labels is None:
labels = unique_labels(y_true, y_pred)
else:
labels = np.asarray(labels)
Here, labels is the optional argument of confusion_matrix() to prescribe an ordering/subset of labels yourself:
cm = confusion_matrix(true_y, pred_y, labels=labels)
Therefore, if labels = [0, 10, 3], cm will have shape (3,3), and the rows/columns can be indexed directly with labels. If you know pandas:
import pandas as pd
cm = pd.DataFrame(cm, index=labels, columns=labels)
Note that the docs of unique_labels() state that mixed types of labels (numeric and string) are not supported. In this case, I'd recommend to use a LabelEncoder. This will save you from maintaining your own lookup-table.
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
y = encoder.fit_transform(y)
# y have now values between 0 and n_labels-1.
# Do some ops here...
...
# To convert back:
y_pred = encoder.inverse_transform(y_pred)
y = encoder.inverse_transform(y)
As the previous answer already mentioned, plot_confusion_matrix() comes in handy to visualize the confusion matrix.

Why there are too many gpus in docker container?

There is 4 GPUs on my host mahchine
[root#c3-sa-i2-20151229-buf023 ~]# nvidia-smi
Wed Jul 12 14:27:40 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.26 Driver Version: 375.26 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla K40m Off | 0000:02:00.0 Off | 0 |
| N/A 23C P8 21W / 235W | 0MiB / 11439MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 1 Tesla K40m Off | 0000:03:00.0 Off | 0 |
| N/A 23C P8 22W / 235W | 0MiB / 11439MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 2 Tesla K40m Off | 0000:83:00.0 Off | 0 |
| N/A 42C P0 105W / 235W | 8336MiB / 11439MiB | 94% Default |
+-------------------------------+----------------------+----------------------+
| 3 Tesla K40m Off | 0000:84:00.0 Off | 0 |
| N/A 23C P8 22W / 235W | 0MiB / 11439MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 2 4148 C python 8330MiB |
+-----------------------------------------------------------------------------+
In docker inspect,I only used 2 GPUs.
"Devices": [
{
"PathOnHost": "/dev/nvidiactl",
"PathInContainer": "/dev/nvidiactl",
"CgroupPermissions": "mrw"
},
{
"PathOnHost": "/dev/nvidia-uvm",
"PathInContainer": "/dev/nvidia-uvm",
"CgroupPermissions": "mrw"
},
{
"PathOnHost": "/dev/nvidia0",
"PathInContainer": "/dev/nvidia0",
"CgroupPermissions": "mrw"
},
{
"PathOnHost": "/dev/nvidia1",
"PathInContainer": "/dev/nvidia1",
"CgroupPermissions": "mrw"
},
{
"PathOnHost": "/dev/fuse",
"PathInContainer": "/dev/fuse",
"CgroupPermissions": "mrw"
}
],
But I can see 4 GPUs in my container.
root#de-3879-ng-1-021909-1176603283-2jpbx:/notebooks# ls /dev | grep nv
nvidia-uvm
nvidia-uvm-tools
nvidia0
nvidia1
nvidia2
nvidia3
nvidiactl
root#de-3879-ng-1-021909-1176603283-2jpbx:/tmp# ./nvidia-smi
Wed Jul 12 06:31:57 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.26 Driver Version: 375.26 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla K40m Off | 0000:02:00.0 Off | 0 |
| N/A 23C P8 21W / 235W | 0MiB / 11439MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 1 Tesla K40m Off | 0000:03:00.0 Off | 0 |
| N/A 23C P8 22W / 235W | 0MiB / 11439MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 2 Tesla K40m Off | 0000:83:00.0 Off | 0 |
| N/A 41C P0 98W / 235W | 8336MiB / 11439MiB | 66% Default |
+-------------------------------+----------------------+----------------------+
| 3 Tesla K40m Off | 0000:84:00.0 Off | 0 |
| N/A 23C P8 22W / 235W | 0MiB / 11439MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
Can I get the device mapping info in docker container?
ex.
host /dev/nvidia0-> container /dev/nvidia0
Can I believe docker inspect info?

Time Series in Stata: var vs. regress

I am seeking your insight on the differences between the var and regress commands in Stata. Given the same variables and the same number of lags, what makes these models different (judging by the differences in their outputs)?
var y x1 x2, lags(1/7)
regress L(1/7).y L(1/7).x1 L(1/7).x2
The series were transformed into stationary beforehand.
var y x1 x2, lags(1/7)
Vector autoregression
Sample: 9 - 159 No. of obs = 151
Log likelihood = -2461.622 AIC = 33.47844
FPE = 7.00e+10 HQIC = 34.01421
Det(Sigma_ml) = 2.90e+10 SBIC = 34.79725
Equation Parms RMSE R-sq chi2 P>chi2
---------------------------------------------------------------
y 22 627.086 0.4632 130.3037 0.0000
x1 22 16.4642 0.4150 107.1156 0.0000
x2 22 34.8932 0.3821 93.37647 0.0000
----------------------------------------------------------------
---------------------------------------------------------------------------------
| Coef. Std. Err. z P>|z| [95% Conf. Interval]
----------------+----------------------------------------------------------------
y |
y |
L1. | -.8034219 .0870606 -9.23 0.000 -.9740576 -.6327862
L2. | -.829339 .1112633 -7.45 0.000 -1.047411 -.611267
L3. | -.6881525 .1268751 -5.42 0.000 -.9368231 -.4394818
L4. | -.5958702 .1316349 -4.53 0.000 -.8538699 -.3378706
L5. | -.4941909 .1285658 -3.84 0.000 -.7461752 -.2422066
L6. | -.3478784 .1130961 -3.08 0.002 -.5695426 -.1262142
L7. | -.1273106 .0892459 -1.43 0.154 -.3022294 .0476083
|
x1 |
L1. | 2.814694 4.697886 0.60 0.549 -6.392995 12.02238
L2. | 13.40258 5.712821 2.35 0.019 2.205654 24.5995
L3. | 13.41822 6.119334 2.19 0.028 1.424542 25.41189
L4. | 7.634082 6.373183 1.20 0.231 -4.857128 20.12529
L5. | 2.001271 5.898859 0.34 0.734 -9.56028 13.56282
L6. | 3.421364 5.569404 0.61 0.539 -7.494468 14.3372
L7. | 4.068799 4.46953 0.91 0.363 -4.691319 12.82892
|
x2 |
L1. | -.5105249 2.210646 -0.23 0.817 -4.843312 3.822262
L2. | -2.108354 2.495037 -0.85 0.398 -6.998537 2.78183
L3. | -1.442043 2.592775 -0.56 0.578 -6.523789 3.639704
L4. | -.9065004 2.620667 -0.35 0.729 -6.042914 4.229913
L5. | -.0001391 2.53355 -0.00 1.000 -4.965806 4.965528
L6. | 2.146481 2.427015 0.88 0.376 -2.610381 6.903343
L7. | -1.118613 2.118762 -0.53 0.598 -5.271309 3.034084
|
_cons | 22.43668 48.04635 0.47 0.641 -71.73243 116.6058
----------------+----------------------------------------------------------------
x1 |
y |
L1. | .0036968 .0022858 1.62 0.106 -.0007833 .0081768
L2. | .0012158 .0029212 0.42 0.677 -.0045097 .0069413
L3. | .0035081 .0033311 1.05 0.292 -.0030208 .010037
L4. | .0032596 .0034561 0.94 0.346 -.0035142 .0100334
L5. | .0005852 .0033755 0.17 0.862 -.0060307 .007201
L6. | -.0018743 .0029693 -0.63 0.528 -.0076941 .0039455
L7. | -.0040389 .0023432 -1.72 0.085 -.0086314 .0005537
|
x1 |
L1. | -.5753736 .1233434 -4.66 0.000 -.8171223 -.3336249
L2. | -.3020477 .1499906 -2.01 0.044 -.5960239 -.0080714
L3. | -.3313213 .1606637 -2.06 0.039 -.6462164 -.0164263
L4. | -.1718872 .1673285 -1.03 0.304 -.4998451 .1560707
L5. | -.1834757 .1548751 -1.18 0.236 -.4870253 .1200739
L6. | .0489376 .1462252 0.33 0.738 -.2376586 .3355337
L7. | .1766427 .1173479 1.51 0.132 -.053355 .4066404
|
x2 |
L1. | -.1051509 .0580407 -1.81 0.070 -.2189086 .0086069
L2. | -.1006968 .0655074 -1.54 0.124 -.229089 .0276954
L3. | -.0906552 .0680736 -1.33 0.183 -.2240769 .0427665
L4. | -.1436015 .0688059 -2.09 0.037 -.2784585 -.0087445
L5. | -.0930764 .0665186 -1.40 0.162 -.2234505 .0372976
L6. | -.1018913 .0637215 -1.60 0.110 -.2267832 .0230006
L7. | -.1194924 .0556283 -2.15 0.032 -.2285218 -.0104629
|
_cons | 1.918878 1.261461 1.52 0.128 -.553541 4.391296
----------------+----------------------------------------------------------------
x2 |
y |
L1. | .0010281 .0048444 0.21 0.832 -.0084667 .0105228
L2. | -.0038838 .0061911 -0.63 0.530 -.0160181 .0082505
L3. | .0035605 .0070598 0.50 0.614 -.0102764 .0173974
L4. | .0041767 .0073246 0.57 0.569 -.0101793 .0185327
L5. | .0007593 .0071538 0.11 0.915 -.013262 .0147806
L6. | -.0027897 .0062931 -0.44 0.658 -.0151239 .0095445
L7. | .0018272 .004966 0.37 0.713 -.0079059 .0115603
|
x1 |
L1. | .3332696 .2614066 1.27 0.202 -.179078 .8456172
L2. | .6160613 .3178811 1.94 0.053 -.0069742 1.239097
L3. | .4139762 .3405009 1.22 0.224 -.2533934 1.081346
L4. | .2837896 .3546259 0.80 0.424 -.4112645 .9788436
L5. | .4448436 .3282329 1.36 0.175 -.1984811 1.088168
L6. | .6417029 .3099009 2.07 0.038 .0343084 1.249098
L7. | .4719593 .2487001 1.90 0.058 -.0154839 .9594025
|
x2 |
L1. | -.7465681 .123008 -6.07 0.000 -.9876594 -.5054769
L2. | -.6760273 .1388325 -4.87 0.000 -.948134 -.4039206
L3. | -.4367948 .144271 -3.03 0.002 -.7195607 -.1540289
L4. | -.4889316 .145823 -3.35 0.001 -.7747393 -.2031238
L5. | -.5310379 .1409755 -3.77 0.000 -.8073447 -.254731
L6. | -.4416263 .1350475 -3.27 0.001 -.7063146 -.1769381
L7. | -.3265204 .1178952 -2.77 0.006 -.5575907 -.09545
|
_cons | 3.568261 2.673465 1.33 0.182 -1.671634 8.808155
---------------------------------------------------------------------------------
regress L(1/7).y L(1/7).x1 L(1/7).x2
Source | SS df MS Number of obs = 151
-------------+------------------------------ F( 20, 130) = 7.23
Model | 49291082.3 20 2464554.11 Prob > F = 0.0000
Residual | 44322342.8 130 340941.099 R-squared = 0.5265
-------------+------------------------------ Adj R-squared = 0.4537
Total | 93613425.1 150 624089.501 Root MSE = 583.9
---------------------------------------------------------------------------------
L.y | Coef. Std. Err. t P>|t| [95% Conf. Interval]
----------------+----------------------------------------------------------------
y |
L2. | -.8074369 .0868829 -9.29 0.000 -.9793244 -.6355494
L3. | -.7857941 .1076428 -7.30 0.000 -.9987525 -.5728357
L4. | -.6747462 .1186733 -5.69 0.000 -.9095271 -.4399654
L5. | -.5758927 .1192639 -4.83 0.000 -.811842 -.3399433
L6. | -.4199846 .1078154 -3.90 0.000 -.6332845 -.2066846
L7. | -.2444889 .0873128 -2.80 0.006 -.4172267 -.071751
|
x1 |
L1. | 9.174249 4.663798 1.97 0.051 -.0525176 18.40102
L2. | 6.026435 5.730833 1.05 0.295 -5.311334 17.3642
L3. | 13.03098 6.057813 2.15 0.033 1.046324 25.01564
L4. | 13.01178 6.318175 2.06 0.041 .5120225 25.51153
L5. | 6.146548 5.91807 1.04 0.301 -5.561646 17.85474
L6. | .8687361 5.610159 0.15 0.877 -10.23029 11.96776
L7. | -.6015264 4.502342 -0.13 0.894 -9.508873 8.30582
|
x2 |
L1. | 2.709283 2.214315 1.22 0.223 -1.671474 7.090041
L2. | 2.947753 2.500195 1.18 0.241 -1.998585 7.89409
L3. | .7449778 2.611172 0.29 0.776 -4.420914 5.910869
L4. | .8159876 2.639117 0.31 0.758 -4.405191 6.037166
L5. | 1.839693 2.54722 0.72 0.471 -3.199677 6.879062
L6. | 2.267241 2.436901 0.93 0.354 -2.553876 7.088358
L7. | 4.198018 2.102467 2.00 0.048 .0385389 8.357497
|
_cons | -3.078699 48.40164 -0.06 0.949 -98.83556 92.67816
---------------------------------------------------------------------------------
For me they have two different specifications .
The first one (VAR) is estimating the impact of the lags of three independent variables to the dependent variable (y, x1, x2) one at time. The second one is estimating the impact of Lags from 2:7 of y + Lags from (1:7) of x1 + Lags from (1:7) of x2 on the dependent variable L(y). So they have two different dependent variables and independent variables on the y side. See equations below (The first three are for var code and the last one is for regress code):
The OLS specification does not take into account the feedback effect that exists between the variables in your model. Although you maybe interested in the effect of say X1 on y, but X1 is also affect by y and its lag values- the feedback effect. for this reason, using OLS will result in spurious regression.

Resources