10.6 test solution.

本文解析了三道编程题目:序列排序、同余方程组求解及字符串中好的回文串数量统计的方法。提供了完整的解决方案及代码实现。

1.排序(sort.in/.out/.cpp)

时间限制

1000ms

空间限制

256MB

【问题描述】

小Z有一个数字序列 a1,a2,an ,长度为 n ,小Z只有一个操作:选定p(1pn),然后把 ap 从序列里拿出来,然后再插入到序列中任意位置。

a 序列为1,2,4,5,3 p=5 ,可以取出 3 ,然后在任意位置插入,可以变为1,2,3,4,5

现在给你一个序列 a ,问你是否可以通过一次操作把整个序列从小到大排好序(变成不降的)。

【输入格式】

第一行一个整数n,第二行有空格隔开的 n 个整数,代表 a 序列。

【输出格式】

如果一次操作可以排好序,输出YES,否则输出NO。

【样例输入】

5
1 2 4 5 3

【样例输出】

YES

【数据规模和约定】

对于 30% 的数据,满足 n1000
对于 60% 的数据,满足 n105
对于 100% 的数据,满足 n106,1ai106


solution

  • 求出最长不下降子序列的长度,小于 n1 输出NO,否则输出YES,正确性显然

code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

template<typename T>
void input(T &x) {
    x=0; T a=1;
    register char c=getchar();
    for(;c<'0'||c>'9';c=getchar())
        if(c=='-') a=-1;
    for(;c>='0'&&c<='9';c=getchar())
        x=x*10+c-'0';
    x*=a;
    return;
}

#define MAXN 1000010

int a[MAXN];
int d[MAXN];

int main() {
    int n;
    input(n);
    for(int i=1;i<=n;i++)
        input(a[i]);
    int len=0;
    d[++len]=a[1];
    for(int i=2;i<=n;++i){
        if(a[i]>=d[len]) d[++len]=a[i];
        else *upper_bound(d+1,d+len+1,a[i])=a[i];
    }
    puts(len<n-1?"NO":"YES");
    return 0;
}

2.同余方程组(mod.in/.out/.cpp)

时间限制

1000ms

空间限制

256MB

【问题描述】

求关于 x 的同余方程组
x%a1=b1
x%a2=b2
x%a3=b3
x%a4=b4
的大于等于 0 的最小正整数解。

【输入格式】

一行 8 个整数,表示 a1,b1,a2,b2,a3,b3,a4,b4

【输出格式】

一行一个整数表示答案

【样例输入】

2 0 3 1 5 0 7 3

【样例输出】

10

【数据规模和约定】

对于 30 的数据, ai40 , 保证 ai 均为素数。
对于 60 的数据, 1ai103 , 保证 ai 均互素。
对于 100 的数据, 0bi<ai,1ai103


solution

  • 裸的中国剩余定理不互质做法

  • 再展开感觉不太好,所以推荐这篇blog

code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

template<typename T>
void input(T &x) {
    x=0; T a=1;
    register char c=getchar();
    for(;c<'0'||c>'9';c=getchar())
        if(c=='-') a=-1;
    for(;c>='0'&&c<='9';c=getchar())
        x=x*10+c-'0';
    x*=a;
    return;
}

#define MAXN 10

ll a[MAXN],m[MAXN];

ll gcd(ll a,ll b) {
    return b?gcd(b,a%b):a;
}

void exgcd(ll a,ll b,ll &x,ll &y) {
    if(!b) {
        x=1,y=0;
        return;
    } else exgcd(b,a%b,y,x);
    y-=(a/b)*x;
    return;
}

ll inv(ll a, ll b) {
    ll d=gcd(a,b);
    if(d!= 1) return -1;
    ll x,y;
    exgcd(a,b,x,y);
    return (x%b+b)%b;
}

bool merge(ll a1, ll m1, ll a2, ll m2, ll &a3, ll &m3) {
    ll d=gcd(m1, m2);
    ll c=a2-a1;
    if(c%d) return false;
    c=(c%m2+m2)%m2;
    m1/=d,m2/=d,c/=d;
    c*=inv(m1,m2);
    c%=m2,c*=m1*d,c+=a1;
    m3=m1*m2*d,a3=(c%m3+m3)%m3;
    return true;
}

ll CRT() {
    ll a1=a[1],m1=m[1];
    for(int i=2;i<=4;i++) {
        ll a2=a[i],m2=m[i];
        ll m3,a3;
        if(!merge(a1,m1,a2,m2,a3,m3))
            return -1;
        a1=a3,m1=m3;
    }
    return (a1%m1+m1)%m1;
}

int main() {
    freopen("mod.in","r",stdin);
    freopen("mod.out","w",stdout);
    for(int i=1;i<=4;i++)
        input(m[i]),input(a[i]);
    cout<<CRT()<<endl;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

3.字符串(str.in/.out/.cpp)

时间限制

1000ms

空间限制

256MB

【问题描述】

如果把一个字符串从头到尾翻转后和原字符串相等,我们称之为回文串,比如“aabaa”、“())(”、“2017102”。

如果一个字符串存在两个出现过的字母出现的次数相等,我们称之为好的字符串。

现在给你一个由小写字母组成的字符串,问在这个字符串的所有连续子串中,好的回文串有多少个。(两个相同的回文串出现在不同位置算多次)。

【输入格式】

一行一个由小写字母组成的字符串。

【输出格式】

一行一个整数,表示答案。

【样例输入】

abcbaabcba

【样例输出】

6

【样例解释】

abcba s[1..5] a,b 出现次数相等
baab s[4..7] a,b 出现次数相等
cbaabc s[3..8] a,b 出现次数相等
bcbaabcb s[2..9] a,c 出现次数相等
abcbaabcba s[1..10] a,b 出现次数相等
abcba s[6..10] a,b 出现次数相等

【数据规模和约定】

len 表示字符串长度。
对于 30% 的数据, len102
对于 60% 的数据, len103
对于 100% 的数据, 1len104


solution

  • 先想暴力,一个最直观的暴力就是找出回文串然后暴力统计每个字母出现的次数,然后枚举两个字符的出现次数是否相等

  • 没错这个思路是可以A的,只不过需要一些优化

  • 求回文串可以用 Manacher 优化到 O(n) ,不会 Manacher 的话,推荐这篇blog

  • 然后我们可以利用回文串的性质

  • 统计的时候只统计一半,计数的时候要加2,这个应该很显然吧

  • 但是因为是只统计一半,回文串长度为奇数的时候要特别注意中间的字符

  • 然后暴力 O(262) 判断是不是好的字符串就好啦

  • 然后这个的总复杂度感觉比较有趣,所以我们来分析一下

  • Manacher O(n) ,之后 n 变成了 2n+1

  • 然后枚举每一个字符是 O(2n+1)

  • 每个字符扩展出来的回文串有 x 个的话,判断是O(262)

  • 所以复杂度大约是 O(n+262|s|) ,其中 |s| 代表回文串的长度

  • 以上是蒟蒻的理解可能不对,欢迎指错

code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 10010

char s[MAXN<<1];
int p[MAXN<<1];
int n,m;

void Manacher() {
    n=strlen(s+1);
    for(int i=n;i>=1;--i)
        s[i*2]=s[i];
    for(int i=1;i<=n*2+1;i+=2)
        s[i]='_';
    int R,pos;
    s[0]='#';
    m=n*2+1;R=pos=0;
    for(int i=1;i<=m;++i){
        if(R<i){
            p[i]=0;
            for(;s[i+p[i]+1]==s[i-p[i]-1];++p[i]);
            R=p[i]+i;
            pos=i;
            continue;
        }
        int j=pos*2-i;
        if(i+p[j]<R) {
            p[i]=p[j];
            continue;
        }
        p[i]=R-i;
        for(;s[i+p[i]+1]==s[i-p[i]-1];++p[i]);
        R=p[i]+i;
        pos=i;
    }
    return;
}

int sum[30];

int calc(int pos,int k) {
    int ans=0;
    memset(sum,0,sizeof(sum));        
    if(s[pos]>='a'&&s[pos]<='z') {
        sum[s[pos]-'a'+1]++;
        pos-=2;
    } else pos--;
    for(;k;pos-=2,k--) {
        bool flag=false;
        sum[s[pos]-'a'+1]+=2;
        for(int i=1;i<=26;i++) {
            if(!sum[i]) continue;
            for(int j=i+1;j<=26;j++)
                if(sum[i]==sum[j]) {
                    ans++;
                    flag=true;
                    break;
                }
            if(flag) break;
        }
    }
    return ans;
}

int main(){
    scanf("%s",s+1);
    Manacher();
    int ans=0;
    for(int i=1;i<=m;++i)
        if(p[i]!=0&&p[i]!=1)
            ans+=calc(i,p[i]>>1);
    printf("%d",ans);
    return 0;
}
Objectives of this Assignment 1. Declare arrays of different types dynamically. 2. Iterate through arrays, processing all of the elements. 3. Write methods with arrays as parameters and return values In this assignment you will create your own class and write the methods in it. The class you write is called P7_2 and it has four methods that build arrays and four methods that process arrays. All methods should be public and static. Create a project called P7_2 and a class named Main in a file called Main.java, then follow the instructions below exactly: 1.Write a method named createDoubles that builds an array of floating point values that represent the squares of the numbers from start to end, in steps of 0.5, inclusive of both boundaries. The numbers start and end should be readed from the starndard input.See the sample input for details. The method has no parameters and returns an array of doubles. You should calculate the length of this array. 2.Write a method called findLargest that takes an array of doubles as a parameter, and returns a double equal to the largest element in the array. 3.Add a main method with the usual signature that instantiates the Main class and tests its methods as follow: public static void main(String[] args) { // Create arrays double[] doubleArray = createDoubles(); // Test processing System.out.printf("%.1f", findLargest(doubleArray)); } Input Specification: enter two numbers which indicates the start number and the end number. Output Specification: For each case, output the largest number in the created array. Sample Input: 10.0 13.0 Sample Ouput: 169.0
03-10
import os import time import itertools import math import numpy as np import scipy as sp import scipy.sparse as sps from scipy.sparse.linalg import splu import torch import torch.nn as nn import pyamg from scipy.sparse import csr_matrix, isspmatrix_csr, diags from pyamg.multilevel import multilevel_solver from warnings import warn from scipy.sparse import csr_matrix, isspmatrix_csr, SparseEfficiencyWarning from pyamg.relaxation.smoothing import change_smoothers device = 'cpu' # ========== 辅助函数 ========== def prolongation_fn(grid_size): res_stencil = np.zeros((3,3), dtype=np.double) k=16 res_stencil[0,0] = 1/k res_stencil[0,1] = 2/k res_stencil[0,2] = 1/k res_stencil[1,0] = 2/k res_stencil[1,1] = 4/k res_stencil[1,2] = 2/k res_stencil[2,0] = 1/k res_stencil[2,1] = 2/k res_stencil[2,2] = 1/k P_stencils = np.zeros((grid_size//2, grid_size//2, 3, 3)) for i in range(grid_size//2): for j in range(grid_size//2): P_stencils[i,j,:,:] = res_stencil return compute_p2(P_stencils, grid_size).astype(np.double) def compute_p2(P_stencil, grid_size): indexes = get_p_matrix_indices_one(grid_size) P = csr_matrix((P_stencil.reshape(-1), (indexes[:, 1], indexes[:, 0])), shape=((grid_size//2) ** 2, (grid_size) ** 2)) return P def get_p_matrix_indices_one(grid_size): K = map_2_to_1(grid_size=grid_size) indices = [] for ic in range(grid_size // 2): i = 2 * ic + 1 for jc in range(grid_size // 2): j = 2 * jc + 1 J = int(grid_size // 2 * jc + ic) for k in range(3): for m in range(3): I = int(K[i, j, k, m]) indices.append([I, J]) return np.array(indices) def map_2_to_1(grid_size=8): k = np.zeros((grid_size, grid_size, 3, 3)) M = np.reshape(np.arange(grid_size ** 2), (grid_size, grid_size)).T M = np.concatenate([M, M], axis=0) M = np.concatenate([M, M], axis=1) for i in range(3): I = (i - 1) % grid_size for j in range(3): J = (j - 1) % grid_size k[:, :, i, j] = M[I:I + grid_size, J:J + grid_size] return k def diffusion_stencil_2d(epsilon=1.0, theta=0.0, type='FD'): eps = float(epsilon) theta = float(theta) C = np.cos(theta) S = np.sin(theta) CS = C*S CC = C**2 SS = S**2 if type == 'FE': a = (-1*eps - 1)*CC + (-1*eps - 1)*SS + (3*eps - 3)*CS b = (2*eps - 4)*CC + (-4*eps + 2)*SS c = (-1*eps - 1)*CC + (-1*eps - 1)*SS + (-3*eps + 3)*CS d = (-4*eps + 2)*CC + (2*eps - 4)*SS e = (8*eps + 8)*CC + (8*eps + 8)*SS stencil = np.array([[a, b, c],[d, e, d],[c, b, a]]) / 6.0 elif type == 'FD': a = -0.5*(eps - 1)*CS b = -(eps*SS + CC) c = -a d = -(eps*CC + SS) e = 2.0*(eps + 1) stencil = np.array([[a, d, c],[b, e, b],[c, d, a]]) return stencil def coo_to_tensor(coo): values = coo.data.astype(np.float64) indices = np.vstack((coo.row, coo.col)) i = torch.LongTensor(indices) v = torch.DoubleTensor(values) shape = coo.shape return torch.sparse_coo_tensor(i, v, torch.Size(shape)).to(device) # ========== 光滑算子 ========== def neural_smoother(net, size, mixed=0): # 返回PyTorch张量而不是SciPy矩阵 if mixed == 1: I = torch.eye(size*size, dtype=torch.double, device=device) x0 = I for conv_layer in net.convLayers1: kernel = conv_layer.weight.detach().view(3, 3) M = toeplitz_conv(kernel, size) x0 = torch.mm(M, x0) return x0 else: I = torch.eye(size*size, dtype=torch.double, device=device) x0 = I for conv_layer in net.convLayers1: kernel = conv_layer.weight.detach().view(3, 3) M = toeplitz_conv(kernel, size) x0 = torch.mm(M, x0) kernel2 = net.convLayers2[0].weight.detach().view(3, 3) M2 = toeplitz_conv(kernel2, size) y = x0 + (2/3) * M2 return y def toeplitz_conv(kernel, size): # 将3x3卷积核转换为Toeplitz矩阵 full_size = size * size M = torch.zeros(full_size, full_size, dtype=torch.double, device=device) for i in range(size): for j in range(size): idx = i * size + j for di in [-1, 0, 1]: for dj in [-1, 0, 1]: ni, nj = i + di, j + dj if 0 <= ni < size and 0 <= nj < size: nidx = ni * size + nj k_val = kernel[di+1, dj+1] M[idx, nidx] = k_val return M # ========== Level 创建 ========== def create_levels(eps, theta, n): mxl = 5 # max levels levels = [] # 创建最细层 s = diffusion_stencil_2d(eps, theta * np.pi / 180, 'FD') * 2 A = pyamg.gallery.stencil_grid(s, (n, n)).tocsr() # 创建第一层 - 使用PyAMG的level类而不是字典 level0 = multilevel_solver.level() level0.A = A level0.N = n level0.l = A.shape[0] levels.append(level0) current_n = n for i in range(1, mxl): # 因为已经有一层,所以从1开始 # 获取当前最细层(最后一层) fine_level = levels[-1] current_n = fine_level.N # 创建限制算子 R = prolongation_fn(current_n) # 插值算子是限制算子的转置 P = R.T * 4 # 存储到当前层(细层) fine_level.R = R fine_level.P = P # 为下一层准备:计算粗网格矩阵 A_coarse = R @ fine_level.A @ P # 创建粗网格层 coarse_level = multilevel_solver.level() coarse_level.A = A_coarse coarse_level.N = current_n // 2 # 网格大小减半 coarse_level.l = A_coarse.shape[0] levels.append(coarse_level) # 检查是否达到最小网格 if coarse_level.N < 8: break return levels # ========== Problem Class ========== class Problem: def __init__(self, eps, theta, grid_size, k=20, initial_ground_truth=None, initial_u=None, levels=None, net_trained=None, mxl=0): self.eps = eps self.theta = theta self.grid_size = grid_size if levels is None: levels = create_levels(eps, theta, grid_size) self.levels = levels N = levels[0].N l = levels[0].l # 初始化真实解 if initial_ground_truth is None: self.ground_truth = torch.rand(l, 1, dtype=torch.double, device=device, requires_grad=False) else: self.ground_truth = initial_ground_truth.detach().requires_grad_(False) # 初始解 if initial_u is None: self.initial_u = torch.rand(l, 1, dtype=torch.double, device=device, requires_grad=False) else: self.initial_u = initial_u.detach().requires_grad_(False) self.k = k self.N = N self.levels = levels self.mxl = mxl self.net_trained = net_trained or [] # 冻结预训练网络的参数 for net in self.net_trained: for param in net.parameters(): param.requires_grad = False # 使用SciPy稀疏矩阵计算右端项 A_sparse = self.levels[0].A gt_numpy = self.ground_truth.detach().cpu().numpy().flatten() f_numpy = A_sparse @ gt_numpy self.f = torch.tensor(f_numpy, dtype=torch.double, device=device).view(-1, 1).requires_grad_(False) def compute_solution(self, net): with torch.no_grad(): # 禁用梯度计算 A_sparse = self.levels[0].A # SciPy稀疏矩阵 b = self.f.detach().cpu().numpy().flatten() # 创建多重网格求解器 solver_a_CNN = multigrid_solver(A_sparse, self.grid_size, {'smoother': 'a-CNN', 'eps': self.eps, 'theta': self.theta}, net, self.net_trained, self.mxl) u_solution = solver_a_CNN.solve(b, maxiter=10, tol=1e-6) return torch.tensor(u_solution, dtype=torch.double, device=device).view(-1, 1) # ========== 求解器 ========== def multigrid_solver(A, size, args, net, net_trained, mxl): solver = geometric_solver(A, prolongation_fn, max_levels=5, coarse_solver='splu') if net_trained!=0: nets = [net]+net_trained else: nets = [net] if args['smoother'] == 'a-CNN': # mxl最大是5 i in range(4) 0 1 2 3 for i in range(mxl-1): # 创建当前层的光滑算子 M = neural_smoother(nets[i], size// (2 ** i )) # 定义光滑函数 - 修改后版本 def relax(A, x, b, M_new=M): # 计算残差 (使用NumPy的稀疏矩阵操作) r = b - A.dot(x) # 转换为PyTorch张量进行矩阵乘法 r_tensor = torch.tensor(r, dtype=torch.double, device='cpu').view(-1, 1) correction = M_new @ r_tensor # 转回NumPy并更新解 x += correction.view(-1).cpu().numpy() # 设置光滑器 solver.levels[i].presmoother = relax solver.levels[i].postsmoother = relax return solver def geometric_solver(A, prolongation_function, presmoother=('gauss_seidel', {'sweep': 'forward'}), postsmoother=('gauss_seidel', {'sweep': 'forward'}), max_levels=5, max_coarse=10, coarse_solver='splu', **kwargs): levels = [multilevel_solver.level()] # convert A to csr if not isspmatrix_csr(A): try: A = csr_matrix(A) warn("Implicit conversion of A to CSR", SparseEfficiencyWarning) except BaseException: raise TypeError('Argument A must have type csr_matrix, or be convertible to csr_matrix') # preprocess A A = A.asfptype() if A.shape[0] != A.shape[1]: raise ValueError('expected square matrix') levels[-1].A = A while len(levels) < max_levels and levels[-1].A.shape[0] > max_coarse: extend_hierarchy(levels, prolongation_function) # 使用MultilevelSolver代替弃用的multilevel_solver ml = pyamg.multilevel.MultilevelSolver(levels, **kwargs) change_smoothers(ml, presmoother, postsmoother) return ml # internal function def extend_hierarchy(levels, prolongation_fn): """Extend the multigrid hierarchy.""" A = levels[-1].A N = A.shape[0] n = int(math.sqrt(N)) R = prolongation_fn(n) P = R.T.tocsr() * 4 levels[-1].P = P # prolongation operator levels[-1].R = R # restriction operator levels.append(multilevel_solver.level()) # Form next level through Galerkin product A = R * A * P A = A.astype(np.float64) # convert from complex numbers, should have A.imag==0 levels[-1].A = A # ========== 神经网络模型 ========== class _ConvNet_(nn.Module): def __init__(self, initial=5, kernel_size=3, initial_kernel=0.1): super(_ConvNet_, self).__init__() self.convLayers1 = nn.ModuleList([ nn.Conv2d(1, 1, kernel_size, padding=kernel_size//2, bias=False).double() for _ in range(5) ]) self.convLayers2 = nn.ModuleList([ nn.Conv2d(1, 1, kernel_size, padding=kernel_size//2, bias=False).double() for _ in range(2) ]) # 初始化权重 initial_weights = torch.zeros(1, 1, kernel_size, kernel_size, dtype=torch.double) initial_weights[0, 0, kernel_size//2, kernel_size//2] = initial_kernel for net in self.convLayers1: net.weight = nn.Parameter(initial_weights.clone()) for net in self.convLayers2: net.weight = nn.Parameter(initial_weights.clone()) def forward(self, x): y1 = x y2 = x for net in self.convLayers1: y1 = torch.tanh(net(y1)) for net in self.convLayers2: y2 = torch.tanh(net(y2)) return y1 + (2/3) * y2 def compute_loss(net, problem_instances): loss = torch.zeros(1, device=device, requires_grad=True) for problem in problem_instances: # 确保计算图连接 with torch.set_grad_enabled(True): u_pred = problem.compute_solution(net) u_true = problem.ground_truth # 确保梯度可以回传 u_pred.requires_grad_(True) u_true.requires_grad_(False) # 计算损失 diff = u_pred - u_true norm_diff = torch.norm(diff) norm_true = torch.norm(u_true) loss = loss + norm_diff / norm_true return loss def chunks(l, n): for i in range(0, len(l), n): yield l[i:i + n] def set_seed(seed): torch.manual_seed(seed) np.random.seed(seed) # ========== AlphaCNN ========== class alphaCNN: def __init__(self, net=None, batch_size=1, learning_rate=1e-6, max_epochs=1000, nb_layers=5, tol=1e-6, stable_count=50, optimizer='SGD', check_spectral_radius=False, random_seed=None, kernel_size=3, initial_kernel=0.1): if random_seed is not None: set_seed(random_seed) if net is None: self.net = _ConvNet_(initial=5, kernel_size=kernel_size, initial_kernel=initial_kernel).to(device) else: self.net = net # 确保网络参数需要梯度 for param in self.net.parameters(): param.requires_grad = True self.learning_rate = learning_rate if optimizer == 'Adadelta': self.optim = torch.optim.Adadelta(self.net.parameters(), lr=learning_rate) elif optimizer == 'Adam': self.optim = torch.optim.Adam(self.net.parameters(), lr=learning_rate) else: self.optim = torch.optim.SGD(self.net.parameters(), lr=learning_rate) self.batch_size = batch_size self.max_epochs = max_epochs self.tol = tol self.stable_count = stable_count def _optimization_step_(self, problem_instances): shuffled_problem_instances = np.random.permutation(problem_instances) for problem_chunk in chunks(shuffled_problem_instances, self.batch_size): self.optim.zero_grad() loss = compute_loss(self.net, problem_chunk) # 检查梯度是否存在 if loss.grad_fn is None: raise RuntimeError("Loss has no gradient. Check the computation graph.") loss.backward() self.optim.step() # 确保梯度被应用 with torch.no_grad(): for param in self.net.parameters(): if param.grad is not None: param -= self.learning_rate * param.grad def fit(self, problem_instances): losses = [] prev_total_loss = compute_loss(self.net, problem_instances).item() convergence_counter = 0 problem_number = len(problem_instances) for n_epoch in range(self.max_epochs): start_time = time.time() self._optimization_step_(problem_instances) total_loss = compute_loss(self.net, problem_instances).item() losses.append(total_loss) if np.abs(total_loss - prev_total_loss) < self.tol * problem_number: convergence_counter += 1 if convergence_counter >= self.stable_count: print(f"Converged after {n_epoch} epochs") break else: convergence_counter = 0 prev_total_loss = total_loss epoch_time = time.time() - start_time if n_epoch % 10 == 0: print(f"Epoch: {n_epoch:>3} Loss: {total_loss:>10.6f} Time: {epoch_time:.2f}s") self.losses = losses print(f"Training completed. Final loss: {total_loss:.6f}") return self # ========== 模型训练 ========== def train_and_save_model(eps, theta, coarsening='full'): n = 33 # 网格大小 # 创建模型目录 model_dir = f'./models/theta_{theta}_eps_{eps}' if not os.path.isdir(model_dir): os.makedirs(model_dir) # 创建层级结构 levels = create_levels(eps, theta, n) # 第一层训练 (最粗层) problem_instances1 = [ Problem(eps, theta, n, k=k, levels=levels, mxl=1) for k in range(1, 13) ] model1 = alphaCNN( batch_size=8, learning_rate=1e-8, max_epochs=1000, nb_layers=5, tol=1e-6, stable_count=10, optimizer='Adam', random_seed=9, initial_kernel=0.1 ) model1.fit(problem_instances1) torch.save(model1.net.state_dict(), os.path.join(model_dir, f'theta_{theta}_eps_{eps}_level1.pth')) # 第二层训练 problem_instances2 = [ Problem(eps, theta, n, k=k, levels=levels, mxl=2, net_trained=[model1.net]) for k in range(1, 15) ] model2 = alphaCNN( batch_size=8, learning_rate=1e-8, max_epochs=1000, nb_layers=5, tol=1e-6, stable_count=10, optimizer='Adam', random_seed=9, initial_kernel=0.02/3 ) model2.fit(problem_instances2) torch.save(model2.net.state_dict(), os.path.join(model_dir, f'theta_{theta}_eps_{eps}_level2.pth')) # 第三层训练 problem_instances3 = [ Problem(eps, theta, n, k=k, levels=levels, mxl=3, net_trained=[model1.net, model2.net]) for k in range(1, 17) ] model3 = alphaCNN( batch_size=8, learning_rate=1e-8, max_epochs=1000, nb_layers=5, tol=1e-6, stable_count=10, optimizer='Adam', random_seed=9, initial_kernel=0.002/3 ) model3.fit(problem_instances3) torch.save(model3.net.state_dict(), os.path.join(model_dir, f'theta_{theta}_eps_{eps}_level3.pth')) # 第四层训练 (最细层) problem_instances4 = [ Problem(eps, theta, n, k=k, levels=levels, mxl=4, net_trained=[model1.net, model2.net, model3.net]) for k in range(1, 20) ] model4 = alphaCNN( batch_size=8, learning_rate=1e-8, max_epochs=1000, nb_layers=5, tol=1e-6, stable_count=10, optimizer='Adam', random_seed=9, initial_kernel=0.002/3 ) model4.fit(problem_instances4) torch.save(model4.net.state_dict(), os.path.join(model_dir, f'theta_{theta}_eps_{eps}_level4.pth')) # 训练模型 if __name__ == "__main__": train_and_save_model(100, 75) 损失值太大,帮我修改代码,检查是否有错误
07-30
你提供的命令中混用了 `kafka-console-producer.sh` 和 `--from-beginning`,这会导致命令出错。因为: - `kafka-console-producer.sh` 是用于**生产消息**的命令,不支持 `--from-beginning` 参数。 - `--from-beginning` 是消费者命令中用于从头读取消息的选项,应该用在 `kafka-console-consumer.sh` 中。 --- ### ✅ 正确的命令:查看 `test-topic-tgy` 的消费情况 如果你想查看 `test-topic-tgy` 这个 Topic 的消费情况(比如有没有消息、消息内容是什么、消费到哪了),可以使用以下命令: #### 1. 查看 Topic 的消息内容(从头开始读取): ```bash kafka-console-consumer.sh --bootstrap-server 10.6.1.111:9092,10.6.1.112:9092,10.6.1.113:9092 \ --topic test-topic-tgy \ --from-beginning \ --max-messages 100 ``` > 说明:该命令会从头读取最多 100 条消息。 --- #### 2. 查看 Topic 的消费组状态(消费者偏移量等): ```bash kafka-consumer-groups.sh --bootstrap-server 10.6.1.111:9092,10.6.1.112:9092,10.6.1.113:9092 \ --describe --group <your-consumer-group> ``` > 替换 `<your-consumer-group>` 为你实际使用的消费者组名,可以查看该组消费每个分区的 `CURRENT-OFFSET`、`LOG-END-OFFSET`、`LAG` 等信息。 --- #### 3. 查看 Topic 的分区和偏移量信息: ```bash kafka-topics.sh --describe --topic test-topic-tgy \ --bootstrap-server 10.6.1.111:9092,10.6.1.112:9092,10.6.1.113:9092 ``` > 该命令会显示该 Topic 的分区数、副本分布、ISR 等信息。 --- ### ✅ 小贴士:快速查看是否有消息 如果你只想快速判断这个 Topic 是否有数据,可以使用: ```bash kafka-console-consumer.sh --bootstrap-server 10.6.1.111:9092,10.6.1.112:9092,10.6.1.113:9092 \ --topic test-topic-tgy \ --max-messages 1 ``` 如果立即输出了一条消息内容,则说明有数据;如果无输出或等待,则可能无消息。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值