包括在C中提前停车

包括在C中提前停车,c,C,当当前精度低于循环中计算的先前精度时,我希望停止for循环以进行验证集 for (int j = 0; j < epochs; j++) { DPRINT("Epoch %d: 000%%", j + 1); DTIME_START_COUNTER; for (int i = 0; i < train_data->m; i++) { X->data[0] = train_data->data[i];

当当前精度低于循环中计算的先前精度时,我希望停止for循环以进行验证集

for (int j = 0; j < epochs; j++) {
    DPRINT("Epoch %d: 000%%", j + 1);
    DTIME_START_COUNTER;
    for (int i = 0; i < train_data->m; i++) {
        X->data[0] = train_data->data[i];
        y->data[0] = train_labels->data[i];
        
        backprop(network, X, y);
        
        if (i % (int)mini_batch_size == 0 || i == train_data->m - 1) {
            DPRINT("\b\b\b\b%03.0f%%", i * 100 / (double)(train_data->m - 1));
            apply_derivation(network, mini_batch_size, training_rate);
        }
    }
    DPRINT("  Eval accuracy: %.2f%%\n",
    network_accuracy(network, eval_data, eval_labels, NULL));
}
for(int j=0;jm;i++){
X->数据[0]=列车_数据->数据[i];
y->数据[0]=列车标签->数据[i];
backprop(网络,X,y);
如果(i%(int)最小批量大小==0 | | i==train_data->m-1){
DPRINT(“\b\b\b%03.0f%%”,i*100/(双精度)(列车数据->m-1));
应用导出(网络、小批量、培训率);
}
}
DPRINT(“评估精度:%.2f%%\n”,
网络精度(网络、评估数据、评估标签、空);
}

是的,但我想在数组中存储精度(来自网络_精度),并比较两个连续迭代的值

您需要做两次,或者只是更精细的控制,进行重构并将代码放入函数中。然后调用该函数

// Put allocation of structs into their own function.
// All the necessary allocation goes in here.
Network *Network_new() {
    // If you simply did `Network network` it would be automatically
    // freed when the function exits. To return a complex value,
    // allocate it on the heap.
    return malloc(sizeof(Network);
}

// Pass in everything it needs. No globals.
// I'm just guessing at the types.
// A proliferation of arguments might suggest the need for a struct
// to gather them together.
Network *do_the_thing(
  Data *train_data,
  Data *train_labels,
  Data *x, Data *y,
  size_t mini_batch_size,
  int training_rate
) {
    // Allocate a new Network struct.
    Network *network = Network_new();
    
    for (int i = 0; i < train_data->m; i++) {
        x->data[0] = train_data->data[i];
        y->data[0] = train_labels->data[i];
        
        backprop(network, x, y);
        
        if (i % (int)mini_batch_size == 0 || i == train_data->m - 1) {
            DPRINT("\b\b\b\b%03.0f%%", i * 100 / (double)(train_data->m - 1));
            apply_derivation(network, mini_batch_size, training_rate);
        }
    }
    
    // Presumably backprop and apply_derivation have changed network.
    return network;
}

// Allocate space for two Network pointers.
Network *networks[2];

for(int i = 0; i < 2; i++) {
  // Save the results to the array.
  networks[i] = do_the_thing(
    train_data,
    train_labels,
    x, y,
    mini_batch_size,
    training_rate
  );
}

// Compare networks[0] and networks[1] as you like.

// Then free them.
for(int i = 0; i < 2; i++) {
  free(network[i]);
}
Network *do_the_thing(
  Data *train_data,
  Data *train_labels,
  Data *x, Data *y,
  size_t mini_batch_size,
  int training_rate,
  Network *network
) {
  // same as before, but don't allocate the network
}

// Allocate space for two Networks.
// Again, there may be additional allocation and initialization required.
Network networks[2];

for(int i = 0; i < 2; i++) {
  do_the_thing(
    train_data,
    train_labels,
    x, y,
    mini_batch_size,
    training_rate,
    &network[i]  // pass in the pre-allocated memory
  );
}

// No need to free, it will be done automatically.
//将结构的分配放入它们自己的函数中。
//所有必要的分配都在这里。
网络*Network_new(){
//如果你只是简单地使用“网络”,它会自动
//函数退出时释放。若要返回复杂值,
//在堆上分配它。
返回malloc(sizeof(网络);
}
//把它需要的东西都交上来,不要全球的。
//我只是在猜测类型。
//大量的参数可能表明需要一个结构
//把他们聚在一起。
网络,做你想做的事(
数据*列车数据,
数据*列车标签,
数据*x,数据*y,
尺寸\u t最小批量\u尺寸,
国际训练费率
) {
//分配一个新的网络结构。
网络*Network=Network_new();
对于(int i=0;im;i++){
x->数据[0]=列车_数据->数据[i];
y->数据[0]=列车标签->数据[i];
backprop(网络,x,y);
如果(i%(int)最小批量大小==0 | | i==train_data->m-1){
DPRINT(“\b\b\b%03.0f%%”,i*100/(双精度)(列车数据->m-1));
应用导出(网络、小批量、培训率);
}
}
//推测backprop和apply_派生已经改变了网络。
返回网络;
}
//为两个网络指针分配空间。
网络*网络[2];
对于(int i=0;i<2;i++){
//将结果保存到数组中。
网络[我]=做那件事(
列车运行数据,
列车标签,
x、 y,,
小批量,
培训费
);
}
//根据需要比较网络[0]和网络[1]。
//然后释放他们。
对于(int i=0;i<2;i++){
免费(网络[i]);
}
或者,传入一个预先分配的返回值。这更灵活,但需要在调用函数的代码中做更多的工作

// Put allocation of structs into their own function.
// All the necessary allocation goes in here.
Network *Network_new() {
    // If you simply did `Network network` it would be automatically
    // freed when the function exits. To return a complex value,
    // allocate it on the heap.
    return malloc(sizeof(Network);
}

// Pass in everything it needs. No globals.
// I'm just guessing at the types.
// A proliferation of arguments might suggest the need for a struct
// to gather them together.
Network *do_the_thing(
  Data *train_data,
  Data *train_labels,
  Data *x, Data *y,
  size_t mini_batch_size,
  int training_rate
) {
    // Allocate a new Network struct.
    Network *network = Network_new();
    
    for (int i = 0; i < train_data->m; i++) {
        x->data[0] = train_data->data[i];
        y->data[0] = train_labels->data[i];
        
        backprop(network, x, y);
        
        if (i % (int)mini_batch_size == 0 || i == train_data->m - 1) {
            DPRINT("\b\b\b\b%03.0f%%", i * 100 / (double)(train_data->m - 1));
            apply_derivation(network, mini_batch_size, training_rate);
        }
    }
    
    // Presumably backprop and apply_derivation have changed network.
    return network;
}

// Allocate space for two Network pointers.
Network *networks[2];

for(int i = 0; i < 2; i++) {
  // Save the results to the array.
  networks[i] = do_the_thing(
    train_data,
    train_labels,
    x, y,
    mini_batch_size,
    training_rate
  );
}

// Compare networks[0] and networks[1] as you like.

// Then free them.
for(int i = 0; i < 2; i++) {
  free(network[i]);
}
Network *do_the_thing(
  Data *train_data,
  Data *train_labels,
  Data *x, Data *y,
  size_t mini_batch_size,
  int training_rate,
  Network *network
) {
  // same as before, but don't allocate the network
}

// Allocate space for two Networks.
// Again, there may be additional allocation and initialization required.
Network networks[2];

for(int i = 0; i < 2; i++) {
  do_the_thing(
    train_data,
    train_labels,
    x, y,
    mini_batch_size,
    training_rate,
    &network[i]  // pass in the pre-allocated memory
  );
}

// No need to free, it will be done automatically.
Network*做什么(
数据*列车数据,
数据*列车标签,
数据*x,数据*y,
尺寸\u t最小批量\u尺寸,
国际培训费率,
网络*网络
) {
//与之前相同,但不分配网络
}
//为两个网络分配空间。
//同样,可能需要额外的分配和初始化。
网络[2];
对于(int i=0;i<2;i++){
做这件事(
列车运行数据,
列车标签,
x、 y,,
小批量,
培训率,
&网络[i]//传入预分配的内存
);
}
//不需要免费,它将自动完成。

问题是什么?你在找
break
?有一个
break
关键字。是break,但我想存储准确性(来自网络\u准确性)在数组中,比较两个连续变量的值iterations@SajidUllah内环或外环的两次连续迭代?是否每次迭代都只更改
网络
评估数据
评估标签
保持不变?什么数组?它是如何声明的?您是如何附加到它的?