Intro
In [ ]:
Copied!
import torch
import torchcrop
from torchcrop.utils.io import make_constant_weather
weather = make_constant_weather(batch_size=2, n_days=150)
model = torchcrop.Lintul5Model()
output = model(weather, start_doy=60)
print(output.yield_) # [B] final storage-organ biomass (g m-2)
print(output.lai.shape) # [B, T+1] LAI trajectory
print(output.dvs.shape) # [B, T+1] development stage trajectory
import torch
import torchcrop
from torchcrop.utils.io import make_constant_weather
weather = make_constant_weather(batch_size=2, n_days=150)
model = torchcrop.Lintul5Model()
output = model(weather, start_doy=60)
print(output.yield_) # [B] final storage-organ biomass (g m-2)
print(output.lai.shape) # [B, T+1] LAI trajectory
print(output.dvs.shape) # [B, T+1] development stage trajectory
In [ ]:
Copied!
import torch
import torch.nn as nn
from torchcrop import Lintul5Model, CropParameters
crop = CropParameters().to(dtype=torch.float64)
crop.tsum1 = nn.Parameter(torch.tensor(900, dtype=torch.float64))
model = Lintul5Model(crop_params=crop).double()
optimizer = torch.optim.Adam([crop.rue], lr=1e-1)
observed_yield = 1200
# Add gradient inspection
for i in range(50):
optimizer.zero_grad()
out = model(weather.to(torch.float64), start_doy=60)
loss = ((out.yield_ - observed_yield) ** 2).mean()
loss.backward()
# Check gradient magnitude
grad = crop.tsum1.grad
print(f"Iter {i}: loss={loss:.6f}, yield={out.yield_.mean():.2f}, grad={grad:.6e}")
optimizer.step()
import torch
import torch.nn as nn
from torchcrop import Lintul5Model, CropParameters
crop = CropParameters().to(dtype=torch.float64)
crop.tsum1 = nn.Parameter(torch.tensor(900, dtype=torch.float64))
model = Lintul5Model(crop_params=crop).double()
optimizer = torch.optim.Adam([crop.rue], lr=1e-1)
observed_yield = 1200
# Add gradient inspection
for i in range(50):
optimizer.zero_grad()
out = model(weather.to(torch.float64), start_doy=60)
loss = ((out.yield_ - observed_yield) ** 2).mean()
loss.backward()
# Check gradient magnitude
grad = crop.tsum1.grad
print(f"Iter {i}: loss={loss:.6f}, yield={out.yield_.mean():.2f}, grad={grad:.6e}")
optimizer.step()