小编xsk*_*xzr的帖子

如何在keras中记录每批的val_loss和loss

我在 keras 中使用回调函数来记录lossval_loss每个时代,但我想按批次做同样的事情。我找到了一个名为 的回调函数on_batch_begin(self,batch,log={}),但我不知道如何使用它。

python machine-learning neural-network keras

16
推荐指数
1
解决办法
5907
查看次数

C++标准库头可以包含C标准头吗?

我只能在标准草案N4582中找到

[res.on.headers/1] C++标头可能包含其他C++标头.

似乎没有指定C++标头是否可以包含C标准标头.

如果允许,即使不包含此标头,使用C标准头中定义的全局名称是否不安全(因为程序可能通过某些C++标准头隐式包含头)?

c c++ c++-standard-library

14
推荐指数
1
解决办法
203
查看次数

为什么 std::span 缺少比较运算符?

不是std::span设计为轻量参考的子区域std::vector/ std::array/普通数组和相似吗?它不应该在其 API 中也包含比较运算符,以与它们保持一致吗?排除背后的原因是什么?

注意:通过比较运算符,我的意思是完整的集合 ( <, <=, ...) 或宇宙飞船<=>

c++ std c++20 std-span

13
推荐指数
1
解决办法
561
查看次数

如何在c ++ 11中使用可变参数模板生成左关联表达式(又称左折叠)?

我想使用c ++模板使用二进制操作聚合(折叠)多个参数.

这样的模板可以使用如下:

fold<add>(100,10,5) 扩展到 add(add(100, 10), 5)

上面显示的特定扩展是"左侧折叠".扩张add(100, add(10, 5))是"正确的折叠".假设add函数执行简单的整数加法,左右两次折叠产生相同的结果,115.

但考虑div执行整数除法(div(a,b)=a/b)的函数.在这种情况下,关联性很重要,左右折叠会产生不同的结果:

fold_left<div>(100,10,5)  --> div(div(100, 10), 5) --> div(10, 5) -->  2
fold_right<div>(100,10,5) --> div(100, div(10, 5)) --> div(100, 2) --> 50
Run Code Online (Sandbox Code Playgroud)

使用可变参数模板生成右关联版本(fold_right)很简单,但我无法弄清楚如何生成左关联版本(fold_left).以下尝试实现会fold_left导致编译器错误:

#include <iostream>

template <typename T> using binary_op = T(*)(const T& a, const T& b);

// The terminal (single-argument) cases of the variadic functions defined later. 
template<typename T, binary_op<T> Operation> inline T fold_right(const T& …
Run Code Online (Sandbox Code Playgroud)

c++ templates fold variadic-templates c++11

11
推荐指数
1
解决办法
607
查看次数

返回临时对象是否在C++中创建临时对象?

考虑C++中的以下代码:

struct A {A(int);};
A foo() {return static_cast<A>(0);}
A x = foo();
Run Code Online (Sandbox Code Playgroud)

这里static_cast<A>(0)通过标准[5.2.9-4]创建一个临时对象,它是一个prvalue.标准[12.2-1]说

类型的临时数在各种上下文中创建:绑定对prvalue的引用(8.5.3),返回prvalue(6.6.3),创建prvalue的转换(4.1,5.2.9,5.2.11,5.4) ,抛出异常(15.1),进入处理程序(15.3),以及一些初始化(8.5).

那么return语句会再次创建一个临时对象吗?

顺便说一下,任何人都可以告诉我标准是否保证隐式类型转换会创建一个临时对象?

c++ return type-conversion temporary-objects

10
推荐指数
2
解决办法
826
查看次数

stream :: seekoff会更新输入序列吗?

[filebuf.virtuals]中:

pos_type seekoff(off_type off, ios_base::seekdir way,
                 ios_base::openmode which
                   = ios_base::in | ios_base::out) override;
Run Code Online (Sandbox Code Playgroud)

效果:让我们来width表示a_­codecvt.encoding().如果is_­open() == false,或者off != 0 && width <= 0,则定位操作失败.否则,如果way != basic_­ios?::?cur还是off != 0,如果最后的操作是输出,然后更新输出序列和写任何不印字序列.接下来,寻找新的位置:如果width > 0,打电话fseek(file, width * off, whence),否则打电话fseek(file, 0, whence).

它没有提到此函数更新输入序列.作为对比,seekpos确实更新输入序列:

pos_type seekpos(pos_type sp,
                 ios_base::openmode which
                   = ios_base::in | ios_base::out) override;
Run Code Online (Sandbox Code Playgroud)

如果可能,改变文件位置以对应存储的位置sp(如下所述).更改文件位置执行如下:

  1. if (om & ios_­base?::?out) != 0,然后更新输出序列并写入任何非移位序列;

  2. 将文件位置设置sp …

c++ buffer stream language-lawyer

10
推荐指数
1
解决办法
168
查看次数

如果已经隐式实例化了特化,则会隐式实例化它吗?

标题中的问题很清楚.更具体地说,请考虑以下示例:

#include <type_traits>

template <typename T>
struct is_complete_helper {
    template <typename U>
    static auto test(U*)  -> std::integral_constant<bool, sizeof(U) == sizeof(U)>;
    static auto test(...) -> std::false_type;
    using type = decltype(test((T*)0));
};

template <typename T>
struct is_complete : is_complete_helper<T>::type {};

// The above is an implementation of is_complete from https://stackoverflow.com/a/21121104/5376789

template<class T> class X;

static_assert(!is_complete<X<char>>::type{}); 
// X<char> should be implicitly instantiated here, an incomplete type

template<class T> class X {};

static_assert(!is_complete<X<char>>::type{}); // #1

X<char> ch; // #2
Run Code Online (Sandbox Code Playgroud)

此代码与GCC和Clang编译.

根据[temp.inst]/1 …

c++ templates language-lawyer template-instantiation implicit-instantiation

10
推荐指数
1
解决办法
147
查看次数

使用std :: bind时,为什么复制ctor被调用了两次?

我正在玩,std::functionstd::bind了解如何复制参数,如果我可以保存一些复制操作.

我理解在使用时std::bind,参数是通过值传递而不是引用(除非std::ref指定).但是,当我运行以下代码段时,复制构造函数被调用两次.有人可以解释原因吗?

struct token
{
    static int i;
    int code;

    token()
        : code(i++)
    {
        cout << __FUNCTION__ << ": " << code << endl;
    }
    virtual ~token()
    {
        cout << __FUNCTION__ << endl;
    }

    token (token const & other)
        : code (other.code)
    {
        cout << "copy ctor: " << code << endl;
    }

    // update -- adding a move ctor
    token (token const && other)
        : code (std::move(other.code))
    {
        cout << "move …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind std-function

9
推荐指数
1
解决办法
229
查看次数

为什么不能用两层list-initializers初始化2D std :: array?

有人可以帮助我理解为什么我的编译器不能/不推断这个?(使用g ++ 7.3)

不起作用:

#include <array>
std::array<std::array<double,2>,2> f() {
 return {{0,0},{0,0}};
}
Run Code Online (Sandbox Code Playgroud)

工作良好:

#include <array>
std::array<std::array<double,2>,2> f() {
 return {std::array<double,2>{0,0},{0,0}};
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,这也失败了:

#include <array>
std::array<std::array<double,2>,2> f() {
 return std::array<std::array<double,2>,2>{{0,0},{0,0}};
}
Run Code Online (Sandbox Code Playgroud)

@ 1201ProgramAlarm指出添加另一组花括号有效:

#include <array>
std::array<std::array<double,2>,2> f() {
 return {{{0,0},{0,0}}};
}
Run Code Online (Sandbox Code Playgroud)

它使用聚合初始化,因为std::array没有用于brace-init-list的构造函数.那没关系,但那么为什么/如何运作?

std::array<double,2> x{1,2};
Run Code Online (Sandbox Code Playgroud)

为什么它处理这种情况但不处理嵌套的情况?

c++ list-initialization stdarray

9
推荐指数
1
解决办法
215
查看次数

模板参数扣除失败和功能参数/参数不匹配

考虑以下程序:

template <class T> struct A { using X = typename T::X; };
template <class T, typename A<T>::X* = nullptr> void f(T, int);
void f(...);
template <class T> void g(T, int, typename A<T>::X* = nullptr); // #
void g(...);

int main() {
  // f(0, nullptr); // error
  g(0, nullptr); // ok       
}
Run Code Online (Sandbox Code Playgroud)

g(0, nullptr)编译而不编译f(0, nullptr)(在Godbolt的 GCC主干和Clang主干下测试).似乎在模板参数推导过程中#,编译器A<int>在发现参数与参数nullptr不匹配时没有实例化int.标准在哪里指定此行为?

c++ templates language-lawyer template-argument-deduction argument-deduction

9
推荐指数
1
解决办法
140
查看次数