若a为何值关于x的方程程(x-a/ax+1)减1无解则a的值为?


文章目录
创建tensor
直接创建
从numpy中获得数据
创建特定的tensor 根据数值要求:
根据矩阵要求:
随机采用生成:
基本运算,加减乘除
乘法
对数运算:
幂函数:
指数运算
截断函数
对比操作:
python number的获取
tensor设置判断:
设置: 通过一些内置函数,可以实现对tensor的精度, 类型,print打印参数等进行设置
tensor的一些用法:
创建tensor
直接创建
torch.tensor(data, dtype=None, device=None,requires_grad=False)data - 可以是list, tuple, numpy array, scalar或其他类型dtype - 可以返回想要的tensor类型device - 可以指定返回的设备requires_grad - 可以指定是否进行记录图的操作,默认为False
需要注意的是,torch.tensor 总是会复制 data, 如果你想避免复制,可以使 torch.Tensor. detach(),如果是从 numpy 中获得数据,那么你可以用 torch.from_numpy(), 注from_numpy() 是共享内存的
torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])tensor([[ 0.1000,
1.2000],[ 2.2000,
3.1000],[ 4.9000,
5.2000]])torch.tensor([0, 1]) # Type inference on data
tensor([ 0, 1])torch.tensor([[0.11111, 0.222222, 0.3333333]],
dtype=torch.float64,
device=torch.device(‘cuda:0’)) # creates a torch.cuda.DoubleTensor
tensor([[ 0.1111, 0.2222, 0.3333]], dtype=torch.float64, device=‘cuda:0’)torch.tensor(3.14159) # Create a scalar (zero-dimensional tensor)
tensor(3.1416)torch.tensor([]) # Create an empty tensor (of size (0,))
tensor([])
从numpy中获得数据
torch.from_numpy(ndarry)
注:生成返回的tensor会和ndarry共享数据,任何对tensor的操作都会影响到ndarry,反之亦然
a = numpy.array([1, 2, 3])t = torch.from_numpy(a)ttensor([ 1,
2,
3])t[0] = -1aarray([-1,
2,
3])
创建特定的tensor 根据数值要求:
torch.zeros(*sizes, out=None, …)# 返回大小为sizes的零矩阵
1
torch.zeros_like(input, …) # 返回与input相同size的零矩阵torch.ones(*sizes, out=None, …) #f返回大小为sizes的单位矩阵torch.ones_like(input, …) #返回与input相同size的单位矩阵torch.full(size, fill_value, …) #返回大小为sizes,单位值为fill_value的矩阵torch.full_like(input, fill_value, …) 返回与input相同size,单位值为fill_value的矩阵torch.arange(start=0, end, step=1, …) #返回从start到end, 单位步长为step的1-d tensor.torch.linspace(start, end, steps=100, …) #返回从start到end, 间隔中的插值数目为steps的1-d tensortorch.logspace(start, end, steps=100, …) #返回1-d tensor ,从10start到10end的steps个对数间隔
根据矩阵要求:
torch.eye(n, m=None, out=None,…) #返回2-D 的单位对角矩阵torch.empty(*sizes, out=None, …) #返回被未初始化的数值填充,大小为sizes的tensortorch.empty_like(input, …) # 返回与input相同size,并被未初始化的数值填充的tensor
随机采用生成:
torch.normal(mean, std, out=None)torch.rand(*size, out=None, dtype=None, …) #返回[0,1]之间均匀分布的随机数值torch.rand_like(input, dtype=None, …) #返回与input相同size的tensor, 填充均匀分布的随机数值torch.randint(low=0, high, size,…) #返回均匀分布的[low,high]之间的整数随机值torch.randint_like(input, low=0, high, dtype=None, …) #torch.randn(*sizes, out=None, …) #返回大小为size,由均值为0,方差为1的正态分布的随机数值torch.randn_like(input, dtype=None, …)torch.randperm(n, out=None, dtype=torch.int64) # 返回0到n-1的数列的随机排列
基本运算,加减乘除
基本运算,加减乘除
乘法
a * b,要求两个矩阵维度完全一致,即两个矩阵对应元素相乘,输出的维度也和原矩阵维度相同
torch.mul(a, b)是矩阵a和b对应位相乘,a和b的维度必须相等,比如a的维度是(1, 2),b的维度是(1, 2),返回的仍是(1, 2)的矩阵
torch.mm(a,b),要求两个矩阵维度是(n×m)和(m×p),即普通二维矩阵乘法
torch.matul(a,b),matmul可以进行张量乘法,输入可以是高维,当输入是多维时,把多出的一维作为batch提出来,其他部分做矩阵乘法
a * b 要求两个矩阵输入维度一致,即矩阵对应元素相乘
当输入是二维矩阵时,torch.mm(a,b)和torch.matul(a,b)是一样的
torch.matul(a,b)可计算高维矩阵相乘,此时,把多出的一维作为batch提出来,其他部分做矩阵乘法
Torch.add(input, value, out=None).add(input, value=1, other, out=None).addcdiv(tensor, value=1, tensor1, tensor2, out=None).addcmul(tensor, value=1, tensor1, tensor2, out=None)torch.div(input, value, out=None).div(input, other, out=None)torch.mul(input, value, out=None).mul(input, other, out=None)
import torchtorch.manual_seed(2020)
# cpu的随机数种子
# torch.cuda.manual_seed()
# 当前gpu的随机数种子
# torch.cuda.manual_seed_all() # 所有gpu的随机数种子# 创建tensor
print('create tensor:')
a = torch.tensor([1, 2, 3])
# 由指定数据创建
print(a) # tensor([1, 2, 3])b = torch.empty(1, 3)
# 创建一个指定大小但未初始化的tensor
print(b) # tensor([[9.7209e+14, 4.5914e-41, 9.7209e+14]])c = torch.zeros(1, 3)
# 创建一个指定大小且值全为0的tensor
print(c)
# tensor([[0., 0., 0.]])
d = torch.ones(1, 3)
# 创建一个指定大小且值全为1的tensor
print(d)
# tensor([[1., 1., 1.]])
e = torch.full((1, 3), 2)
# 创建一个指定大小且值全为指定值的tensor
print(e)
# tensor([[2., 2., 2.]])f = torch.rand(1, 3)
# 创建一个指定大小且值服从(0,1)间的均匀分布的tensor
print(f)
# tensor([[0.4869, 0.1052, 0.5883]])
g = torch.randn(1, 3)
# 创建一个指定大小且值服从标准正态分布的tensor
print(g)
# tensor([[ 0.6983, -0.3339, -0.3049]])
h = torch.randint(low=1, high=10, size=(1,3))
# 创建一个指定大小且值是指定区间整数的tensor
print(h)
# tensor([[8, 5, 6]])# torch.tensor与numpy.ndarray/list的转换
print('\ntorch.tensor and numpy.ndarray / list')
import numpy as npnda_list = [1, 2, 3]
nda = np.array(nda_list)
# list转numpy.ndarray
print(nda)
# [1 2 3]
nda_tensor = torch.from_numpy(nda)
# numpy.ndarray转torch.tensor
print(nda_tensor)
# tensor([1, 2, 3], dtype=torch.int32)
nda_arr = nda_tensor.numpy()
# torch.tensor转numpy.ndarray
print(nda_arr) # [1 2 3]
print(nda_tensor.tolist())
# [1, 2, 3];torch.tensor转list# 一些常用操作
x = torch.tensor([-1, 1, 1, 1])
y = torch.tensor([3, 3, 3, 3])print('\nsize:')
print(x.size(), type(x.size()))
# torch.Size([4]) <class 'torch.Size'>;torch.Size是tuple的子类print('\nabs:')
print(torch.abs(x)) # 取绝对值# 加法
print('\naddition:')
print(x + y)
print(torch.add(input=x, alpha=1, other=y))
# 加法:output = input + alpha*other
print(x.add(y), x)
# tensor([2, 4, 4, 4]) tensor([-1,
1,
1,
1])
x.add_(y)
# 所有带_后缀的操作均为in-place操作
print(x)
# tensor([2, 4, 4, 4])#
减法(类比加法)
print('\nsubtraction:')
print(torch.sub(input=y, alpha=1, other=x))
# tensor([ 1, -1, -1, -1])
print(y.sub(x))
# tensor([ 1, -1, -1, -1])# 元素乘法
print('\nelement-wise multiplication:')
print(x * y)
# tensor([ 6, 12, 12, 12])
print(torch.mul(x, y))
# tensor([ 6, 12, 12, 12])
print(x.mul(y), x) # tensor([ 6, 12, 12, 12]) tensor([2, 4, 4, 4])
x.mul_(y)
# tensor([ 6, 12, 12, 12])
print(x)
# tensor([ 6, 12, 12, 12])# 除法:类比元素乘法
print('\ndivision:')
print(torch.div(x, y))
# tensor([2, 4, 4, 4])
print(x.div_(y))
# tensor([2, 4, 4, 4])# 矩阵乘法
# 对于高维的Tensor(dim>2),定义其矩阵乘法仅在最后的两个维度上,要求前面的维度必须保持一致
# 运算操作只有torch.matmul()和@
print('\ntensor multiplication:')
m = torch.rand(3, 4, 5)
n = torch.rand(3, 5, 4)
print(torch.matmul(m, n).size())
# torch.Size([3, 4, 4])
print((m @ n).size())
# torch.Size([3, 4, 4])# 对于二维和三维的张量各有一个专用的乘法操作:
p = torch.tensor([[1, 2]])
# 注意方括号的数量
q = torch.tensor([[2], [1]])
print(torch.mm(p, q))
# 二维;tensor([[4]])
print(torch.bmm(m, n).size())
# 三维;torch.Size([3, 4, 4])# 幂运算
print('\npower:')
print(p ** 2)
# tensor([[1, 4]])
print(torch.pow(p, 2))
# tensor([[1, 4]])
print(p.pow(2), p)
# tensor([[1, 4]]) tensor([[1, 2]])
p.pow_(2)
print(p)
# tensor([[1, 4]])r = torch.tensor([4., 4.])
print(torch.sqrt(r))
# tensor([2., 2.])
print(r.sqrt(), r)
# tensor([2., 2.]) tensor([4., 4.])
r.sqrt_()
print(r)
# tensor([2., 2.])# 指数和对数运算
print('\nexponent and logarithm:')
print(torch.exp(r))
# tensor([7.3891, 7.3891])
print(r.exp(), r)
# tensor([7.3891, 7.3891]) tensor([2., 2.])
r.exp_()
print(r) # tensor([7.3891, 7.3891])print(torch.log(r))
# tensor([2., 2.]);log2, log10用法相同
print(r.log(), r)
# tensor([2., 2.]) tensor([7.3891, 7.3891])
r.log_()
print(r)
# tensor([2., 2.])# 近似计算
print('\napproximate:')
z = torch.tensor([3.49, 3.51])
print(torch.floor(z), torch.ceil(z), torch.trunc(z), torch.frac(z))
# 向上取整,向下取整,取整数,取小数
# tensor([3., 3.]) tensor([4., 4.]) tensor([3., 3.]) tensor([0.4900, 0.5100])
print(z.floor(), z.ceil(), z.trunc(), z.frac())
# tensor([3., 3.]) tensor([4., 4.]) tensor([3., 3.]) tensor([0.4900, 0.5100])
print(torch.round(z), z.round())
# 四舍五入
# tensor([3., 4.]) tensor([3., 4.])
print(torch.clamp(z, 3.4, 3.5), z.clamp(3.4, 3.5))
# 限幅
# tensor([3.4900, 3.5000]) tensor([3.4900, 3.5000])
# 上述属性同样有带后置下划线的in-place版本# 统计信息
print('\nstatistics:')
print(torch.max(z), torch.min(z), torch.mean(z), torch.var(z), torch.median(z))
# 最大值,最小值,均值,方差,中位数
# tensor(3.5100) tensor(3.4900) tensor(3.5000) tensor(0.0002) tensor(3.4900)
print(z.max(), z.min(), z.mean(), z.var(), z.median())
# tensor(3.5100) tensor(3.4900) tensor(3.5000) tensor(0.0002) tensor(3.4900)
对数运算:
torch.log(input, out=None)
# y_i=log_e(x_i)torch.log1p(input, out=None)
#y_i=log_e(x_i+1)torch.log2(input, out=None)
#y_i=log_2(x_i)torch.log10(input,out=None)
#y_i=log_10(x_i)
幂函数:
torch.pow(input, exponent, out=None)
# y_i=input^(exponent)
指数运算
torch.exp(tensor, out=None)
#y_i=e^(x_i)torch.expm1(tensor, out=None)
#y_i=e^(x_i) -1
截断函数
torch.ceil(input, out=None)
#返回向正方向取得最小整数torch.floor(input, out=None)
#返回向负方向取得最大整数torch.round(input, out=None) #返回相邻最近的整数,四舍五入
torch.trunc(input, out=None) #返回整数部分数值
torch.frac(tensor, out=None) #返回小数部分数值torch.fmod(input, divisor, out=None) #返回input/divisor的余数
torch.remainder(input, divisor, out=None) #同上
对比操作:
torch.eq(input, other, out=None)
#按成员进行等式操作,相同返回1torch.equal(tensor1, tensor2) #如果tensor1和tensor2有相同的size和elements,则为truetorch.eq(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))tensor([[ 1,
0],[ 0,
1]], dtype=torch.uint8)torch.eq(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))tensor([[ 1,
0],[ 0,
1]], dtype=torch.uint8)torch.ge(input, other, out=None) # input>= other
torch.gt(input, other, out=None) # input>other
torch.le(input, other, out=None) # input=<other
torch.lt(input, other, out=None) # input<other
torch.ne(input, other, out=None) # input != other 不等于
torch.max() # 返回最大值
torch.min() # 返回最小值
torch.isnan(tensor) #判断是否为’nan’
torch.sort(input, dim=None, descending=False, out=None) #对目标input进行排序
torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) #沿着指定维度返回最大k个数值及其索引值
torch.kthvalue(input, k, dim=None, deepdim=False, out=None) #沿着指定维度返回最小k个数值及其索引值
torch.dot(tensor1, tensor2) #返回tensor1和tensor2的点乘torch.mm(mat1, mat2, out=None) #返回矩阵mat1和mat2的乘积torch.eig(a, eigenvectors=False, out=None) #返回矩阵a的特征值/特征向量torch.det(A) #返回矩阵A的行列式torch.trace(input) #返回2-d 矩阵的迹(对对角元素求和)torch.diag(input, diagonal=0, out=None) #torch.histc(input, bins=100, min=0, max=0, out=None) #计算input的直方图torch.tril(input, diagonal=0, out=None) #返回矩阵的下三角矩阵,其他为0torch.triu(input, diagonal=0, out=None) #返回矩阵的上三角矩阵,其他为0
获取python number:由于pytorch 0.4后,
python number的获取
统一通过 .item()方式实现:
a = torch.Tensor([1,2,3])a[0]
#直接取索引返回的是tensor数据tensor(1.)a[0].item()
#获取python number
tensor设置判断:
torch.is_tensor()
#如果是pytorch的tensor类型返回truetorch.is_storage() # 如果是pytorch的storage类型返回ture
这里还有一个小技巧,如果需要判断tensor是否为空,可以如下
a=torch.Tensor()len(a)0len(a) is 0True
设置: 通过一些内置函数,可以实现对tensor的精度, 类型,print打印参数等进行设置
torch.set_default_dtype(d)
#对torch.tensor() 设置默认的浮点类型torch.set_default_tensor_type() # 同上,对torch.tensor()设置默认的tensor类型torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32
torch.float32torch.set_default_dtype(torch.float64)
torch.tensor([1.2, 3]).dtype # a new floating point tensor
torch.float64torch.set_default_tensor_type(torch.DoubleTensor)torch.tensor([1.2, 3]).dtype # a new floating point tensor
torch.float64torch.get_default_dtype() #获得当前默认的浮点类型torch.dtypetorch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)#)
tensor的一些用法:
torch.lerp(star, end, weight) : 返回结果是out= star t+ (end-start) * weight
torch.rsqrt(input) : 返回平方根的倒数
torch.mean(input) : 返回平均值
torch.std(input) : 返回标准偏差
torch.prod(input) : 返回所有元素的乘积
torch.sum(input) : 返回所有元素的之和
torch.var(input) : 返回所有元素的方差
torch.tanh(input) :返回元素双正切的结果
torch.equal(torch.Tensor(a), torch.Tensor(b)) :两个张量进行比较,如果相等返回true
torch.max(input): 返回输入元素的最大值
torch.min(input) : 返回输入元素的最小值
element_size() :返回单个元素的字节
torch.from_numpy(obj),利用一个numpy的array创建Tensor。注意,若obj原来是1列或者1行,无论obj是否为2维,所生成的Tensor都是一阶的,若需要2阶的Tensor,需要利用view()函数进行转换。
torch.numel(obj),返回Tensor对象中的元素总数。
torch.ones_like(input),返回一个全1的Tensor,其维度与input相一致
torch.cat(seq, dim),在给定维度上对输入的张量序列进行连接操作
torch.chunk(input, chunks, dim)在给定维度(轴)上将输入张量进行分块
torch.squeeze(input),将input中维度数值为1的维度去除。可以指定某一维度。共享input的内存
torch.unsqeeze(input, dim),在input目前的dim维度上增加一维
torch.clamp(input, min, max),将input的值约束在min和max之间
torch.trunc(input),将input的小数部分舍去
展开全部
如果没理解错的话式子是这样么?a*(X^2)-(a+1)x+1<0这个式子要讨论当A=0时,式子变成X+1>0,所以X>-1当A<0时,根据求根公式X=(1/2a)*{(a+1)±根号里[(a+1)的平方-4a]}整理得到X=1/2或者X=A分之1因为此时A<0所以X的解集是X<1/A或者X>1/2(因为现在A<0,图象开口向下)当A>0时同理这时候 1/A < X < 1/2再把3个解综合一下,分情况叙述
楼上的这里错了
分类讨论思想解:ax平方-(a+1)x+1<0当a=0时 -(x+1)<0所以 x<1
不等式都解错了
本回答被提问者采纳展开全部
分类讨论思想解:ax平方-(a+1)x+1<0当a=0时 -(x+1)<0所以 x<1
当a不等于0时求根公式 x1= -b+根号(b平方-4ac)/2ax2= -b-根号(b平方-4ac)/2a又因为a=a b=-(a+1) c=1所以x1=1 x2=1/a验证因为 根判别式=b平方-4ac=(a-1)的平方所以有根 成立
展开全部题目是(ax)^2-(a+1)x+1<0吗?是的话。。。解如下:因式分解得:(ax-1)(x-1)<0讨论a的取值范围当0<a<1时,1/a>1则不等式解为:1<x<1/a当a<0,或a>1时,1/a<1则不等式解为:1/a<x<1当a=0时,不等式解为x>1收起 更多回答(2)

我要回帖

更多关于 a为何值关于x的方程 的文章

 

随机推荐