# 微分

def f(x):
return x**3-1/x
x = np.arange(0.1,3,0.1)
plot(x, [f(x), 4*x-4], legend=[‘f(x)’, ‘Tangent line (x=1)’])

[6x_1, 5e^{x_2}]

x/||x||_2

df/da = df/dxdx/da + df/dydy/da + df/dzdz/da
df/db = df/dx
dx/db + df/dydy/db + df/dzdz/db

1 Like

return (hasattr(X, “ndim”) and X.ndim == 1 or isinstance(X, list)
and not hasattr(X[0], “len”))

8 Likes

# as following code is going to import essential library.

import collections
import hashlib
import math
import os
import random
import re
import shutil
import sys
import tarfile
import time
import zipfile
from collections import defaultdict
import pandas as pd
import requests
from IPython import display
from matplotlib import pyplot as plt
from matplotlib_inline import backend_inline

# as above is the essential libary.

import torch
import numpy as np
import torchvision
from PIL import Image
from torch import nn
from torch.nn import functional as F
from torch.utils import data
from torchvision import transforms

# as following is to try the plotting coding

def f(x):
print(‘Ffunction done’)
return 3x**2 - 4x

def use_svg_display():
backend_inline.set_matplotlib_formats(‘svg’)
print(‘use svg display is done’)

def set_figsize(figsize=(3.5, 2.5)):
use_svg_display()
plt.rcParams[‘figure.figsize’] = figsize
print(‘set figsize is done’)

#save
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
axes.set_xlim(xlim)
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()
print(‘set axes is done’)

#save
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale=‘linear’, yscale=‘linear’,
fmts=(’-’, ‘m–’, ‘g-.’, ‘r:’), figsize=(3.5, 2.5), axes=None):
if legend is None:
legend = []

``````set_figsize(figsize)
print('set fig size done')
axes = axes if axes else plt.gca()
print('get axes done')
def has_one_axis(X):
return (hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list)
and not hasattr(X[0], "__len__"))
if has_one_axis(X):
X = [X]
print('x has one axis')
if Y is None:
X, Y = [[]] * len(X), X
print('Y is none')
elif has_one_axis(Y):
Y = [Y]
print('y has one axis')
if len(X) != len(Y):
X = X * len(Y)
print('x and y len is not same')
axes.cla()
for x, y, fmt in zip(X, Y, fmts):
if len(x):
axes.plot(x, y, fmt)
print('len(x)is not 0')
else:
axes.plot(y, fmt)
print('len(x) is 0')
set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
print('plot function is done')
``````

x = np.arange(0, 3, 0.1)
plot(x, [f(x), 2 * x - 3], ‘x’, ‘f(x)’, legend=[‘f(x)’, ‘Tangent line (x=1)’])

print(‘finished’)

python格式化输出，.5f表示保留五位小数，多次迭代后数太小了，前五位小数都是0