在ImageView上的GradientColor

时间:2019-03-22 12:16:51

标签: android

我有一个ImageView,我想在上面放一个渐变色。

所以,我使用FrameLayout,在其中放置一个ImageView和一个View。

import torch

class Network(torch.nn.Module):
    def __init__(self):
        super(Network, self).__init__()

        self.linear = torch.nn.Linear(10, 1)


    def forward(self, x):
        return self.linear(x)

loss = torch.nn.MSELoss()

for i in range(0, 1000):
    torch.manual_seed(i)
    X = torch.randn(100, 10)
    y = torch.randn(100, 1)





    model=Network()
    model.train()
    optimizer=torch.optim.SGD(model.parameters(),lr=1.)
    optimizer.zero_grad()
    output = loss(model(X), y)
    output.backward()

    torch_grads=[]
    for p in model.parameters():
        torch_grads.append(p.grad.detach().data)



    #df/dW = (-2X.T*y+2*X.T*b+2*X.T*X*W)/nsamples 
    #df/db = (2*b-2*y+2*W.T*X.T).mean() (the mean comes from implicit broadcasting of b)

    theory_grad_w = (-2 * torch.matmul(X.t(), y)
                     +2 * torch.matmul(torch.t(X), torch.ones((X.shape[0], 1)))* list(model.parameters())[1]
                     +2 * torch.matmul(torch.matmul(X.t(), X), list(model.parameters())[0].t())
                     ) / float(X.shape[0])

    theory_grad_w = theory_grad_w.t()


    theory_grad_b = torch.mean(2 * list(model.parameters())[1]- 2 * y+ 2 * torch.matmul((list(model.parameters())[0]), torch.t(X)))

    theory_grads = [theory_grad_w, theory_grad_b]

    b=all([torch.allclose(u, d) for u, d in zip(torch_grads, theory_grads)])
    if not(b):

      print("i=%s, pass=%s"%(i, b))

这是我的渐变色:

   <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0">

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/bg_login" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_login_gradient"/>
</FrameLayout>

问题是这样的: 我想要这个结果: Target

但是,我得到了: Bug

请帮助;)

预先感谢

1 个答案:

答案 0 :(得分:0)

将背景图像放入drawable-xxhdpi可以正常工作。